OnCollision Script...

I’ve got this script I’m trying to only happen when an object with a “waypointcube” tag collides with it. The debug I put in the script is not working. Therefore, what is not setup correctly?

function OnCollisionEnter (collision : Collision) {
	if (collision.gameObject.CompareTag ("waypointcube")){
		Debug.Log("hit");
		}
}

Put logging before the if which outputs collision.gameObject.tag to see wether the function is called at all and what you're hitting.

Did you try logging the tag there to see if the tag "waypointcube" ever comes up? Maybe it's just a matter of case sensitivity (e.g. "WayPointCube")?

2 Answers

2

Try

collision.gameObject.tag == "waypointcube"`

instead of:

collision.gameObject.CompareTag ("waypointcube")

That has the same effect, see e.g. here: http://answers.unity3d.com/questions/40975/why-does-comparetag-exist.html

I actually never used ‘CompareTag’.
When I use

if (gameObject.tag == "mytag")
  badabing();

the comparison works, so I’d suggest you do this first, to be sure that everything’s setup correctly.

Of course you too might want to put the debug at function start, to actually print the Collision tag.

Anyways, regarding tag comparison, since it’s most certainly a bitwise operation (as for layers)

the correct function call should be

function OnCollisionEnter (collision : Collision) {
    if (collision.gameObject.tag == CompareTag()){
       Debug.Log("hit");
       }
}

if I understand its use correctly.

That is not correct usage of CompareTag(), see the reference...