- Home /
OnTriggerEnter Influencing all GameObjects of the Same Type
Hi,
I have a perfectly working trigger, but I have coded several cubes in the level to be destroyed when they collide with the trigger. The problem that I am having is that when the first collider hits the trigger, all the cubes coded for to be destroyed are all destroyed. What I want is for each cube to be destroyed only when they collide with the trigger.
Here is a snippet:
var lifeTime = 3.0;
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Trigger1"){
}
}
function Awake()
{
Destroy (gameObject, lifeTime);
}
Thank you
Answer by twoface262 · Apr 19, 2012 at 06:53 PM
Well what you are doing is destroying everything in your "Awake" function. Try this var lifeTime = 3.0;
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Trigger1"){
Destroy (gameObject, lifeTime);
}
}
The cubes are not being destroyed with this new code. I don't know why. This code makes sense to me...
Answer by Kryptos · Apr 19, 2012 at 06:57 PM
You are destroying the cube in Awake
method, that's why!
Awake is called on every object once they are created.
Try with this code instead:
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Trigger1"){
Destroy(gameObject);
}
}
The cubes are not being destroyed with this new code. I don't know why. This code makes sense to me...