Hi!, I have a problem with a JavaScript in my game:
function OnTriggerEnter (other : Collider) {
switch (other.name){
case “iCubo”:
Debug.Log(“iCubo”);
//ecubos++;
break;
case “uCubo”:
Debug.Log(“uCubo”);
//acubos ++;
break;
}
Destroy(other.gameObject);
}
I dont know why this code doesn’t works. can anyone help me?
How can i do a Switch Case, using the name of the object which the character collides?
Before your switch put:
Debug.Log("other name: " + other.name);
Make sure that the name is what you expect it to be.
Hi Afisicos,
Everything works fine with the script. Be sure the object you are colliding with has the Is Trigger checked in the Inspector. Also, double check that you have either a Character Controller or Rigidbody Component attached to your player object. I’ve attached a working example using your script in a scene. 458327–16032–$switch case example.unitypackage (4.61 KB)
If you’d like further instruction on it, I’ve posted a video covering switch and case statements here.
Let me know if you still have issues after that. Thank you.
thanks guys, my problem was in the name of the objects. Now it works perfectly.
I was trying with Debug.Log("other name: " + other.name); after the switch case and I discovered my noob problem xD.
thanks