Scoreboard not updating

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;

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 Ball)
{
    if (Ball.gameObject.name == "leftGoal")
    {
        rightPaddleScore++;
    }

    if (Ball.gameObject.name == "rightGoal")
    {
        leftPaddleScore++;
    }

    Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString();

    print(rightPaddleScore + " , " + leftPaddleScore);
}

}

Start by adding a Debug.Log at the start of your OnTriggerEnter2D method to see if you are actually hitting something.


It could be something like:

Debug.Log ("Found object: " + Ball.name); 
// You should change Ball to the default name collision or something like hit
// cuz it's not the Ball what you are receiving, it's the hit info of any object that 
// the gameobject with your script collided with.

If there is no hit info, them you should check out why not, it could be that you are missing some configuration in your gameobjects, like having a collider with isTrigger disabled and missing a rigidbody2D component.

try

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 Ball)
{
if (Ball.gameObject.name == “leftGoal”)
{
rightPaddleScore++;
}
if (Ball.gameObject.name == “rightGoal”)
{
leftPaddleScore++;
}
Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString();
print(rightPaddleScore + " , " + leftPaddleScore);
}
void Update()
{
OnTriggerEnter2D;
}