-1
\$\begingroup\$

When I play the game it works perfectly fine but it shows an error in the console when pressing buttons but it is still working just causing a bit of lag.

This is the error: NullReferenceException: Object reference not set to an instance of an object uiManager.Update () (at Assets/Scripts/uiManager.cs:31)

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

public class uiManager : MonoBehaviour {

public Button[] buttons;
public Button resume;
public Button pause;
public Text scoreText;
public Text hiScore;
public Text newHighScore;
public Text optionsResetPrompt;
int score;
bool gameOver;

// Use this for initialization
void Start () {
    pause.gameObject.SetActive(true);
    resume.gameObject.SetActive(false);
    gameOver = false;
    score = 0;
    InvokeRepeating("scoreUpdate", 0.5f, 0.25f);
    hiScore.text = PlayerPrefs.GetInt("highscore").ToString();
}

// Update is called once per frame
void Update () {
    scoreText.text = "" + score;
}

void scoreUpdate(){
    if (gameOver == false) {
        score += 1;
    }  
}
public void gameOverActivated(){
    gameOver = true;
    pause.gameObject.SetActive(false);
    resume.gameObject.SetActive(false);
    foreach (Button button in buttons) {
        button.gameObject.SetActive(true);
    }
    int highestScore = PlayerPrefs.GetInt("highscore", 0);
    if (score > highestScore){
        PlayerPrefs.SetInt("highscore", score);
        newHighScore.gameObject.SetActive(true);
        }

}

public void Play() {
    SceneManager.LoadScene("level1");
}

public void Menu()
{
    SceneManager.LoadScene("Menu");
    if (Time.timeScale == 0)
    {
        Time.timeScale = 1;
    }
}

public void Reset()
{
    PlayerPrefs.DeleteKey("highscore");
    optionsResetPrompt.gameObject.SetActive(true);
}

public void Options()
{
    SceneManager.LoadScene("Options");
}

public void Retry()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    if (Time.timeScale == 0){
        Time.timeScale = 1;
    }
}

public void Pause() {

    if (Time.timeScale == 1) {
        Time.timeScale = 0;
        pause.gameObject.SetActive(false);
        resume.gameObject.SetActive(true);
        foreach (Button button in buttons)
        {
            button.gameObject.SetActive(true);
        }
    }

}
public void Resume() {

     if (Time.timeScale == 0) {
        Time.timeScale = 1;
        pause.gameObject.SetActive(true);
        resume.gameObject.SetActive(false);
        foreach (Button button in buttons)
        {
            button.gameObject.SetActive(false);
        }
    }
}
}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ So it's throwing this exception at the lines pause.gameObject.SetActive(true); and scoreText.text = "" + score;? Did you drag the corresponding GameObjects to the entries in the Inspector? Just a tip: Don't do scoreText.text = "" + score; in Update(), this sets the text every frame. If you only set the score in your InvokeRepeating anyway (so 4x per second), just changet the text in there too. ;) \$\endgroup\$ – Neph Oct 4 '18 at 12:29
0
\$\begingroup\$

Such an error is caused, because the object: Text scoreText , is not assigned. I suppose you have an empty object in your scene, where you have placed the uiManager Script as a component. There, you will see all of your public variables and you will have to assign a value to them.

In this case, in the variable scoreText of uiManager script component, you will need to assign(drag n drop) your actual text object in your editor.

Cheers

\$\endgroup\$
  • \$\begingroup\$ Well that is not the case since I already dragged and drop the text object in the inspector. That's why I'm a bit confused on why it is giving me the error. \$\endgroup\$ – DeeMastah Oct 4 '18 at 14:47
  • \$\begingroup\$ Your variables have lost their instances somehow, as seen also in Start() line 21. Are you loading different Scene maybe and you lose the instances? \$\endgroup\$ – Nikos Giannaras Oct 4 '18 at 14:53
  • \$\begingroup\$ I think I get it now the error only pops up when I press the Menu/Options button which it loads a different scene. On that scene the text objects are not present that's why it's giving such error. \$\endgroup\$ – DeeMastah Oct 4 '18 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.