-3

after watching this https://www.youtube.com/watch?v=uE2RJAyVaHE&list=PLDj6B2jXbus1v9zD_IdPnGcim3uwSDELI&index=4 and I did what is said, I got this error NullReferenceException: Object reference not set to an instance of an object

this is the scoreboard code

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

public class Count_Score : MonoBehaviour
{
    public Text ScoreBoard;
    public GameObject ball;

    private int Bat_1_Score = 0;
    private int Bat_2_Score = 0;

    // Start is called before the first frame update
    void Start()
    {
        ball = GameObject.Find("Ball");
    }

    // Update is called once per frame
    void Update()
    {
        if (ball.transform.position.x >= 35f)
        {
            Bat_1_Score++;
        }
        if (ball.transform.position.x <= -35f)
        {
            Bat_2_Score++;
        }

        ScoreBoard.text = Bat_1_Score.ToString() + " - " + Bat_2_Score.ToString();

        print(Bat_1_Score + " , " + Bat_2_Score);
    } 

can you tell me what I did wrong?

Botje
  • 26,269
  • 3
  • 31
  • 41
  • Welcome. Please make a title which is useful for others with the same issue, as SO strives to be a repository for questions and answers rather than a help-desk. Currently it is very generic. Thank you. – user438383 Sep 16 '21 at 13:34
  • well, I have send a YouTube link where I was following tutorial, it's called pong, and I'm having a problem with an error when I started the game with scoreboard – Linedol trainer channel gammin Sep 16 '21 at 13:40
  • Your title is very confusing ;) if you speak about the game pong but make a typo and write `ping` people are trying to figure out what your code hadls to do with networking ;) – derHugo Sep 16 '21 at 16:08

1 Answers1

0

I think the code cannot get your ball for some reason on this line:

ball = GameObject.Find("Ball");

Are tou sure your Ball gameobject is actually called "Ball"?

Does the case match?

enter image description here

Else try to add this if statement to your code:

if (!ball)
{
    ball = GameObject.Find("Ball");
}

To make sure ball is not already assigned in the inspector.

NickGames
  • 312
  • 1
  • 18