- Home /
Score not updating
public class CountScore : MonoBehaviour {
public Text Scoreboard;
public GameObject ball;
private int leftPaddleScore = 0;
private int rightPaddleScore = 0;
// Start is called before the first frame update
void Start()
{
ball = GameObject.Find("Ball");
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "leftGoal")
{
rightPaddleScore++;
}
if (collision.gameObject.name == "rightGoal")
{
leftPaddleScore++;
}
Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString();
print(rightPaddleScore + " , " + leftPaddleScore);
}
}
What GameObject in your scene is this script attached to? You can't use OnTriggerEnter2D on any object other than the two that are supposed to collide with one another and maybe that's why your script isn't working
thank you so much I had to put the script on my ball object as well
This is a repeated question that already has answers, if you still have this issue, you should check your previous question and keep asking there.
you didn't help me very much when I had the same question
I actually told you that a way to achieve it could be adding the script to your paddle and just checking with a tag if the ball was the object that hit your paddle trigger. If you had to add this script to your ball as well, you are actually not understanding how OnTriggerEnter2D works. The object that has this script must has his collider isTrigger checked and a rigidbody, and any other object with a collider can activate this method, if one of both objects have a rigidbody, it will work.
The code seems ok, the problem must be in the hierarchy, review where the scripts are attached and the game objects names :P