- Home /
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);
}
}
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; }
Answer by tadadosi · May 19, 2020 at 08:45 PM
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.
I definitely think its connecting, because every time the ball hits a goal I get this message:
0 , 0 UnityEngine.$$anonymous$$onoBehaviour:print(Object) CountScore:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/CountScore.cs:39)
I think your OnTriggerEnter2D code should be in a script for the actual Paddle that you want your ball to hit, each Paddle with its own script and they send a message to the scoreboard after they get a hit by the ball.
And you just simple check if what hits the paddle was the ball by adding a tag to the ball.
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Ball"))
{
// score++;
}
}
Did you check is trigger on your collider.
Answer by emmanuelsegun2008 · May 19, 2020 at 07:24 PM
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; }
like this?
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;
}
}
I got this error when I made the changes
Assets\Scripts\CountScore.cs(44,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
then you might have to find a workaround I don't know anything else.