- Home /
what else could be wrong, OnTriggerEnter
ok I want to use OnTriggerEnter but cannot get it to work. The script is basic with a debug to check if it works:
public void OnTriggerEnter (Collider other) {
Debug.Log("entered");
}
This script is placed on a still object, that has a collider where is trigger is set as true.
my second object moves forward the still - this object has both ridgid body and a collider.
Have been looking around but really cannot figure out why it doesn't work when I run it (game is in 2d don't know if that changes anything)
Answer by tanoshimi · May 17, 2015 at 06:28 PM
If your game is 2D, you need to be using Rigidbody2D, Collider2D, and OnTriggerEnter2D. Unity's 2D physics and 3D physics sysyems are completely separate.
https://unity3d.com/learn/tutorials/modules/beginner/2d/rigidbody2d
I had 2d colliders and rigidbodies to begin with - sorry it wasn't stated. Tried using OnTriggerEnter2D in the code, but i still don't see my debug. The video mentioned collision detection, so tried to set that as continuous, but still the same result
Please show the actual code and a screenshot of the components attached to the objects you're using now.
ok first picture
code looks like this and is on the cookie gameobject:
public class PlayerHealth : $$anonymous$$onoBehaviour {
public int curHealth = 3;
private SpriteRenderer myRenderer;
public Sprite[] CookieLife;
void Start (){
myRenderer = GetComponent<SpriteRenderer>();
}
public void ChangeSprite() {
switch(curHealth){
case 6:
myRenderer.sprite = CookieLife[5];
break;
case 5:
myRenderer.sprite = CookieLife[4];
break;
case 4:
myRenderer.sprite = CookieLife[3];
break;
case 3:
myRenderer.sprite = CookieLife[2];
break;
case 2:
myRenderer.sprite = CookieLife[1];
break;
case 1:
myRenderer.sprite = CookieLife[0];
break;
case 0:
Destroy( this.gameObject );
// end game
break;
}
}
public void OnTriggerEnter2d (Collider other) {
Debug.Log("entered");
print ("ola");
//if (other.tag == "Bug") {
// curHealth -= 1;
// ChangeSprite ();
//}
}
}
You've got the name and method signature wrong. Ins$$anonymous$$d of:
public void OnTriggerEnter2d (Collider other)
it should be:
void OnTriggerEnter2D(Collider2D other)
(note the capital "D" in "Enter2D" and also the Collider2D parameter). http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html
Answer by abdoubelbala · May 17, 2015 at 08:32 PM
tanoshimi has right , you have to use rigidbody2d and boxcollidder2d and you have to change the script it will be like this
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("entered");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613195834im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player ignores tilemap collider on fall 1 Answer
Distribute terrain in zones 3 Answers
OnMouseDrag issue, Event System 0 Answers
how to setting highscore with second in unity 2d? (C#) 1 Answer