Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

How to access variable from other script in runtime?

$
0
0
Hi lads, I'm new to Unity. Now I have a prefab of enemy and my gun is using ray-cast to detect if hits the enemy. I have two js files. One is fireBullet.js which detects ray-cast and other one is enemy.js which contains enemy information. This is firebullet.js: #pragma strict var maxGunRange : float = 10000000; var bulletHole1 : GameObject; var bloodSplat : GameObject; var enemy : GameObject; var frontHit : float = 0.00001; function Start () { } function Update () { var hit : RaycastHit; //Debug.DrawLine(transform.position, hit.point, Color.red); if(Physics.Raycast(transform.position, transform.forward, hit, maxGunRange)){ if(bulletHole1 && hit.transform.tag == "ground"){ Instantiate(bulletHole1, hit.point + (hit.normal * frontHit), Quaternion.LookRotation(hit.normal)); } if(bulletHole1 && hit.transform.tag == "enemy"){ Instantiate(bloodSplat, hit.point + (hit.normal * frontHit), Quaternion.LookRotation(hit.normal)); //enemy.GetComponent(enemy).enemyHP -= 20; } } Destroy(gameObject); } This is enemy.js: #pragma strict var distance : float; var Target : Transform; var detectRange : float = 25; var attackRange : float = 5; var movementSpeed : float = 5; var damp : float = 4; var bloodOut : GameObject; var enemyHP : float = 100; function Start () { Target = Camera.main.transform; } function Update () { if(enemyHP <= 0){ Destroy(gameObject); } } I want to reduce enemy HP when hit but I don't know how to do it. I know I should use getComponent to access the data and modify it but failed(I comment the line). Could someone tell me how to fix it. In addtion, am I write the correct code to destroy the enemy prefab when HP <= 0? if(enemyHP <= 0){ Destroy(gameObject); } Thank you!

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>