- Home /
OnCollisionEnter doesnt seem to work, i want to make objects dissappear when i come in contact with them?
var piecesCollected : int = 0;
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag=="piece") {
Destroy(gameObject);
}
piecesCollected++;
}
function OnGUI () { GUI.Label (Rect (10, 10, 100, 20), piecesCollected.ToString()); }
I have tagged my objects "piece", but nothings seems to be happening, help needed please. Im new to unity
does one of the two colliding objects have a rigid body?!?!
Format your code, then maybe I'll answer the question
Do you have any errors in the console at all? What actually happens with the above code running? Do both items have a collider applied to them from the inspector?
According to Unity Script Reference: "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."
Answer by mattssonon · Jul 01, 2013 at 09:18 AM
You should probably do this:
Destroy(theCollision.gameObject);
Instead of this:
Destroy(gameObject);
Also, one of the colliders must have a Rigid Body component.
Answer by umangindianic · Jul 02, 2013 at 09:24 AM
Add the Rigidbody in your Player Object or in which object by you collect pieces.
Check the tag in which object you added. Tag must be assigned with piece objects.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to check if an object is colliding with another from another script ? 1 Answer
C# Collision Detection Help 0 Answers
public variable audio clip not initializing sound 0 Answers
bullet decals have wrong rotation C# 2 Answers