Question by
cupcakehox · May 16, 2018 at 05:57 AM ·
collider2dontriggerenter2d
Multiple colliders on the same script
Hi guys,
I am doing a simple ball tossing game and i am kinda struggling with something pretty easy I think. So i basically got a bunch of balls in which i will apply force and put them in some boxes.I got 2 boxes, the 1st one is supposed to increase the score, the 2nd one has some spikes that will destroy the balls and decrease the score. So basically what I want is the same script so the same OnTriggerEnter2D method to handle these BoxColliders.I tried using tags and stuff but seems like it ain't work.
void OnTriggerEnter2D(Collider2D collision)
{
rb = collision.GetComponent<Rigidbody2D>();
if (collision.transform.tag=="Box" && rb.gameObject.tag=="Ball")
{
rb.tag = "stop";
print("Ball got into the box");
count--;
HighscoreController.Score++;
print(count + " balls left");
}
else if(collision.transform.tag=="Spike" && rb.gameObject.tag == "Ball")
{
count--;
Debug.Log("Ball destroyed");
Destroy(collision.gameObject);
HighscoreController.Score--;
}
The variable rb is the rigidbody of the ball.
Thanks in advance
Comment
Your answer
Follow this Question
Related Questions
BoxCollider2D OnTriggerEnter2D 1 Answer
OnTriggerEnter2D problem 1 Answer
OnTriggerEnter2D at high velocity 0 Answers