NullReferenceException in UI Text component

409 views Asked by At

I am getting this NullReferenceException whenever I click on the menu button after the game ends . Now because of this error My game is getting hang and I am not able to replay it again .So I have to restart it :

This is the error:

NullReferenceException: Object reference not set to an instance of an object
UiManager.Update () (at Assets/UiManager.cs:21)

This is the code:

public class UiManager : MonoBehaviour {
    float score=0;
    public Text scoreText;
    public Text endText;
    public Button[] buttons;
    public Text TIMER; 
    float time=30f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        time -=Time.deltaTime;
        TIMER.text = "TIME: " + (int)time;
        if (time < 1) {
            GameOverF ();
            Time.timeScale = 0;
        }
    }

    public void IncrementScore(){

        score++;
        scoreText.text = "SCORE: " + score;
    }

    public void IncrementBadScore(){
        score-=11;
        scoreText.text = "SCORE: " + score;
    }
    public void IncrementGoodScore(){
        score+=11;
        scoreText.text = "SCORE: " + score;
    }


    public void IncrementRedScore(){

            score-=2;
            scoreText.text = "SCORE: " + score;
            }

    public void GameOverF(){
        endText.gameObject.SetActive (true);
        foreach (Button button in buttons) {
            button.gameObject.SetActive (true);

        }


    }

    public void exit(){
        Application.Quit ();
    }

    public void Play(){
        Application.LoadLevel ("level1");
    }
    public void Menu(){

        Application.LoadLevel ("menu");
    }


}

This is the line where the error is coming : Error

and this is Inspector Of my game object : inspector

Everything is set fine still this error is coming and I am not able to find out why ? Please help !

1

There are 1 answers

1
utkdub On

In Menu() put Time.timeScale=1;

THATS IT

actually the hanging problem was coming due to the fact that when the game ends or timer goes off I made Time.timeScale value 0. So the next time I restarted it , It was already paused.

the hanging problem was not due to NullReferenceException when u change scene the Text component gets destroyed and when called causes this exception !