- Home /
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.
when Debug.Log("hit") is put before the "IF" statement the Log appears whenever the object hit anything. I only want it to trigger when the chosen object is collided.
Did you try logging the tag there to see if the tag "waypointcube" ever comes up? $$anonymous$$aybe it's just a matter of case sensitivity (e.g. "WayPointCube")?
Answer by roamcel · Nov 03, 2011 at 05:30 AM
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...
Answer by Ludiares.du · Nov 02, 2011 at 08:34 PM
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