Ive got multiple enemies of the same prefab in my scene. My problem is that when the first enemy gets within 4 units of the player it blows up and does what it is supposed to do. However, when the next ones get to the player they only come up to him and stop. They dont take away from player health and they dont explode. Code (csharp): var player : Transform; var explosion : ParticleEmitter; function Update () { if(player) { var dist = Vector3.Distance(player.position, transform.position); if(dist <= 4){ Destroy(gameObject); PlayerHealth.playerhealth -= 4; Instantiate(explosion, transform.position, transform.rotation); } } }
How are these objects created in the scene? It seems to me if they are instantiated and you are not fulfilling "player" then they wont know what the player is. hence it's not a good thing to incorporate "player" into a script, or if you do, tag the player as "Player" and use something like this: Code (csharp): function Start(){ player=gameObject.FindWithTag("Player") }
Ah, they are already in the scene when you start the game. I made sure to check that. While i was testing the game i made sure to check the inspector of each enemy and everything was correct.
Ah ok. I changed the script and it works now. Thank you for the help. Code (csharp): var explosion : ParticleEmitter; function Update () { var player = gameObject.FindWithTag ("Player"); if(player) { var dist = Vector3.Distance(player.transform.position, transform.position); if(dist <= 4){ Destroy(gameObject); PlayerHealth.playerhealth -= 4; Instantiate(explosion, transform.position, transform.rotation); } } }