- Home /
How to access a bool of a specific clone in a collision
So I have a ship flying around and there are these enemies spawning continuously. My problem is that I wanted to make it so that the enemies are safe to fly into for a second after they spawn. The problem I have is that these enemies are all clones and I don't know how to access the individual bool 'isDangerous' for each individual enemy on collision. Thankyou for your help
If isDangerous is not static, each object has their own isDangerous. Just check the variable from the script on the object the player collided with. Show us some code and we can help more
Answer by Neamtzu · Mar 27, 2015 at 03:42 PM
void OnCollisionEnter(Collision collision) {
if(collision.collider.gameObject.tag == "Enemy") {
var script = collision.collider.gameObject.GetComponent <EnemyScriptComponent> ();
if(script.isDangerous) {
//kill him
}
else {
//ignore it
}
}
}
This should do the job. Instead of tag, you can use name, or if you don't have other colliders in scene, you can ignore that if. Instead of EnemyScriptComponent use your enemy script. IsDangerous should be public but not static.
Your answer
Follow this Question
Related Questions
Loading new level after killing enemies 3 Answers
Mario-Like Enemy Death System Not Working 1 Answer
Make object move over other object in 3D space 2 Answers
Trying to spawn enemies 1 Answer
Enemy not taking damage on collisions. 2 Answers