- Home /
Cannot remove a prefab using OnCollisionEnter function.
Hi, I have a set of spawners generating prefabs randomly and an object below that is supposed to remove the prefab when it collides with it. This is the script Iam using:
function OnCollisionEnter ( collision : Collision){
if (collision.gameObject.name == "Coin(Clone)"){
Destroy(collision.gameObject);}
}
Both objects have box colliders enabled, I've tried changing "is trigger", changing the name of the object (coin, coin(Clone)), tried to change the script by adding transform variable and detecting collision with a prefab directly instead of the name but it is still not removing the prefabs. What am I missing?
yes, I even tried using variables and stuff, the problems isnt there..
Put in a debug.log statement at different spots through the code so you can work out where it gets to... That will help work out what is wrong. The code itself should work
Answer by steakpinball · Feb 27, 2013 at 02:01 PM
Moving objects typically need to have a rigid body attached for collisions to occur. Check the collision matrix on http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html to verify whether it is needed.
And I suggest using tags to identify objects in this manner.
Adding rigid body to either od the objects is not helping. Nor is the tag identifying..
Your answer
Follow this Question
Related Questions
Destroy a GameObject 2 Answers
Checking to see if two objects are colliding. 2 Answers
Emit Particle on Collision Issue 0 Answers
How can I make my truck drive on terrain and on bridge 1 Answer
How to instantiate on collision? 0 Answers