Unity Object Reference not set to an instance of an Object

Asked

Viewed 1,190 times

0

I’m doing a quiz in Unity and I have an error that I can’t solve, I’ve researched similar errors but I can’t identify, this message appears when I’m running the quiz

Nullreferenceexception: Object Reference not set to an instance of an Object Gamecontroller.Start() (at Assets/Gamecontroller.Cs:39)

this is the Gamecontroller

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

public class GameController : MonoBehaviour {

public Text textoPergunta;
public Text textoPontos;
public Text textoTimer;
public Text highScoreText;

public SimpleObjectPool answerButtonObjectPool;
public Transform answerButtonParent;
public GameObject painelDePerguntas;
public GameObject painelFimRodada;

private DataController dataController;
public RoundData rodadaAtual;
private QuestionData[] questionPool;

private bool rodadaAtiva;
private float tempoRestante;
private int questionIndex;
private int playerScore;

List<int> usedValues = new List<int>();

private List<GameObject> answerButtonGameObjects = new List<GameObject>();



// Use this for initialization
void Start () {


    dataController = FindObjectOfType<DataController>();


    rodadaAtual = dataController.GetCurrentRoundData();                   //ERRO Some quando tiro essas linhas      
    questionPool = rodadaAtual.perguntas;                                 //ERRO   
    tempoRestante = rodadaAtual.limiteDeTempo;                            //ERRO

    UpdateTimer();

    playerScore = 0;
    questionIndex = 0;
    ShowQuestion();
    rodadaAtiva = true;

}

// Update is called once per frame
void Update () {

    if (rodadaAtiva)
    {
        tempoRestante -= Time.deltaTime;
        UpdateTimer();

        if (tempoRestante <= 0)
        {
            endRound();
        }
    }


}


// Funções
private void UpdateTimer()
{
    textoTimer.text = "Timer: " + Mathf.Round(tempoRestante).ToString();
}


private void ShowQuestion()
{
    RemoveAnswerButtons();

    int random = Random.Range(0, questionPool.Length);
    while (usedValues.Contains(random))
    {
        random = Random.Range(0, questionPool.Length);
    }
    QuestionData questionData = questionPool[random];
    usedValues.Add(random);
    textoPergunta.text = questionData.textoDaPergunta;


    for (int i = 0; i < questionData.respostas.Length; i++)
    {
        GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();

        answerButtonGameObject.transform.SetParent(answerButtonParent);

        answerButtonGameObjects.Add(answerButtonGameObject);

        AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
        answerButton.Setup(questionData.respostas[i]);
    }
}

private void RemoveAnswerButtons()
{
    while(answerButtonGameObjects.Count > 0)
    {
        answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
        answerButtonGameObjects.RemoveAt(0);
    }
}

public void AnswerButtonClicked(bool estaCorreto)
{
    if (estaCorreto)
    {
        playerScore += rodadaAtual.pontosPorAcerto;
        textoPontos.text = "Score: " + playerScore.ToString();
    }

    if(questionPool.Length > questionIndex + 1)
    {
        questionIndex++;
        ShowQuestion();
    }
    else
    {
        endRound();
    }

}
    public void endRound()
    {
        rodadaAtiva = false;

    painelDePerguntas.SetActive(false);
    painelFimRodada.SetActive(true);
    }

public void ReturnToMenu()
{
    SceneManager.LoadScene("Menu");
}

}

I realized that every time I take the datacontroller.getcurrentRoundData he stops this error, but I can’t see where the error is in this function or in the subsequent ones, because this GetCurrentRoundData is like this:

public RoundData GetCurrentRoundData()
{
    return todasAsRodadas[rodadaIndex];
}

I don’t know if the error is really there and I can’t identify, what I checked was that taking the lines from the footballThis stop to give this error, now how to solve I do not know, if saying something the codes are zipped at the link below the Dropbox, I’m following a tutorial but the code is apparently identical to the tutorial: tutorial link

link to files in Dropbox

1 answer

1


Hello. The reason you are giving this error is because it does not find the reference object and because of this error. To solve this problem first exchange the Datacontroller line for a Gameobject and leave it in public:

 public GameObject objeto;

Then when calling the object you use the Getcomponent<>();

DataController dataController = objeto.GetComponent<DataController>();

Ready, so you can call the methods of the other script, but you will have to call it all the time in other methods of Gamecontroller whenever you want to use.

  • it didn’t work because the error only disappears when I take the lines related to the Rodadatual, that dataController is not making any difference in the error, I already took these lines to test, I did what Oce said and keeps giving exactly the same error, I tried to demonstrate more in the question to see if it becomes clearer

  • You created a gameobject, put the Datacontroller script on it and then make the call?

  • solved, reset the tutorial from scratch and should have some typo, I could not identify what it was, but now it is running 100% vlw help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.