- Home /
Collision detection between two objects?
I used this code:
void OnCollisionEnter(Collision Collider){
if(transform.collider.gameObject.tag == "dna"){
DNA += 1.0f;
Destroy(collider.gameObject);
}
}
The expected outcome is that when the object this script is on touches an object with the "dna" tag, it will add to the "DNA" variable, and destroy the object with the "dna" tag. Neither of these things work, any ideas or suggestions?
Answer by DragonSaige · Aug 26, 2012 at 04:40 PM
A collider is a sort of system of it's own, don't use it as variable names.Here's the code I would use
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag =="dna")
{
DNA +=1.0f;
Destroy(other.gameObject);
}
}
At times, when you really want the collision to detect you can change the settings in rigidbody component.I would recommend changing the colliding mode to continuous dynamic, doing so will make unity detect collision that happen very fast. Also there can be a bit of errors in my script (gameobject ins$$anonymous$$d of gameObject) so don't copy and paste that, write it on your own if you're not doing so already