Question by
NinjaRubberBand · Oct 17, 2015 at 08:42 AM ·
raycastdestroygamobject
Destroy(GameObject) problem
void Update () {
RaycastHit hit;
Vector3 forward = transform.TransformDirection (transform.up) * 1;
Debug.DrawRay (transform.position,transform.up,Color.green);
if(Physics.Raycast (transform.position,(transform.up), out hit)) {
Debug.DrawRay (transform.position,transform.up,Color.red);
if(hit.transform.gameObject.tag == "Enemy"&& Input.GetMouseButtonDown(0)) {
shoot = true;
print ("SHOOOt");
AIisDead = true;
Destroy (hit.GameObject);
}
´(This is a script attached to a gun) When the raycast hits a object with the tag enemy, that object needs to be destroyed. But i cant really find out how to do it. the "hit" is that object the raycast is "touching". And that is the object i want to destroy if i press the mouse button and the object is tagged enemy.
I tried doing in the way that from my enemys script, i destroy if the AIisDead from this script, is true. But that destroyed all the enemys in the scene.
Comment
Answer by TeohRIK · Oct 17, 2015 at 09:26 AM
is the AlisDead is static variable? Because I having this problem before, but I found out is because I put the variable as a static variable, so the value on other enemy get affected