Collision detection between two objects? (50191)

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?

1 Answer

1

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);
	}

}

Still doesn't work.

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 instead of gameObject) so don't copy and paste that, write it on your own if you're not doing so already