- Home /
Check Trigger Collision's Name/Tag
I'm making hitboxes for my game and as a simple test I want them to turn yellow whenever on hitbox touches another.
To start off, I created an onTriggerEnter function
function OnTriggerEnter(collision : Collider){
gameObject.renderer.material.color = Color.yellow;
}
This works perfectly fine. If a hitBox touches a trigger, it turns yellow. But when I add an if statement that refers to the collision's game object, it doesn't work.
function OnTriggerEnter(collision : Collider){
if(collision.gameObject.name == "hitBox"){
gameObject.renderer.material.color = Color.yellow;
}
}
I've tried different variations with "name", "tag", with and without "gameObject", but nothing happens. Is there something that I'm missing? Am I referencing the wrong thing? Any help is appreciated.
Do the collider belongs to the GameObject attached to it or is it a child? Also make sure they have kinematic rigidbodies.
can you do a DebugLog(collision.gameObject.name) and see what it is printing, this might help you work out what is wrong. You should be able to see what you should be comparing it too.
CompareTag("Tag") works nicely.
If it doesnt work either, there is something wrong elsewhere; Like forgetting to check the isTrigger box on the Collider or forgetting to add a non-kinematic rigidbody somewhere.
kinematic rigidbodies working sloved my problem http://answers.unity3d.com/questions/1000365/unity5-c-ontriggerenter-not-working.html
Answer by SepM · Jul 09, 2015 at 05:05 PM
Thank you for your help, everyone! What solved my problem was adding a kinematic Rigidbody to my hit boxes. I realized that the reason why it had worked without the tag check was because they were touching other objects that happened to have their own Rigidbodies. I'm surprised I didn't even consider this. Now by adding Rigidbodies to the hit boxes, their collisions are finally registering. Thank you again!