Search code examples
c#unity-game-enginetextnullreferenceexceptionscene2d

How to display Debug.Log messages on Text.UI after restarting the same scene?


I'm working on a simple little game in Unity where the objective is to use a floating hand to guide the ball into the basket and every time the ball enters the basket, the game resets due to an hidden collider with a trigger inside the basket.

The feature I'm trying to implement:

Every time the ball goes into the basket the text.UI updates to reflect your new score, beginning with 0 points and the score increments by 1 for every slam dunk.

The issue:

How do I convert the "Debug.Log" into a text.UI?

I was only successful in updating the score on the Unity console and I wasn't able to convert these events to the text.UI. The text.UI GameObject I've created only displays the text "New Game" and never gets updated.

Update: I've created a new script to solve this and I got this error:

NullReferenceException: Object reference not set to an instance of an object ChangingText.Start () (at Assets/Scripts/ChangingText.cs:12)

The process:

1. Creating a GameObject and script to keep data after scene restarts.

I've created a script to keep the score after restarting the the same scene, I have only one scene. I've attached this script to the game object: "GameController" and that's how was able to keep the score updated.

The name of the scene is:

"DunkingPractice"

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

public class GameControl : MonoBehaviour
{
    // Giving it a name called "Control", any other script can interact with it
    public static GameControl Control;

    public int score;

    // Called before Start()
    private void Awake()
    {
        // If there's a control already, delete this
        // If there's no control, make this the control object
        if (Control == null)
        {
            Control = this;
            DontDestroyOnLoad(gameObject); // Don't destory the object when a scene is loaded
        }
        else if (Control != this)
        {
            Destroy(gameObject);
        }
    }
}

Image that I've included to demonstrate this:

Creating "GameController" GameObject and Script

2. Creating an hidden trigger collider GameObject inside the basket with a scenemanager.loadscene inside the script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class RestartTrigger : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Ball")
        {
            SceneManager.LoadScene(0);
        }
    }
}

Image that I've included to demonstrate this:

Creating a trigger collider and restart trigger

3. Creating a script to Keep score and adding this component to the aforementioned trigger collider

Notice that the script refers to the Game Control script I've created earlier.

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

public class KeepingScore : MonoBehaviour
{
    static void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Ball")
        {
            GameControl.Control.score++;
            if (GameControl.Control.score == 1)
            {
                Debug.Log("You have " + GameControl.Control.score + " point");
            }
            else if (GameControl.Control.score != 1)
            {
                Debug.Log("You have " + GameControl.Control.score + " points");
            }
        }
   
    }
}

Here's another image that I've included:

Creating a script to keep score and attaching it to the trigger field

4. Creating a Text.UI on screen and creating a new script to change text only for an error to appear

This is the script that produces the NullReferenceException error:

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

public class ChangingText : MonoBehaviour
{
    public Text scoreText;

    void Start()
    {
        scoreText.text = GameControl.Control.score.ToString();
    }

}

Here's another image to demonstrate:

Creating a text object

Here's a screen recording I've made to show how my scene currently looks like:

https://www.screencast.com/t/JUBsUkHuHgHC

Do you have any suggestions?

Thank you


Solution

  • NullReferenceException is happened if one of your instance is null and you are trying to modify it, in the log error, it show that :

    Object reference not set to an instance of an object ChangingText.Start ()

    It means that your scoreText instance does not connect to any UI and it's null. To resolve that, just simply create text UI gameObject and drag it into the 'scoreText' field in the object that is assigned with ChangingText script