Unity Object reference not set to an instance of an object

0

I'm doing a Quiz on Unity and I'm having an error that I can not solve, I already looked for similar errors but I can not 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've noticed that every time I% s and datacontroller.getcurrentRoundData it with this error, but I can not see where the error is in that function or in subsequent ones, since this GetCurrentRoundData looks like this:

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

I do not know if the error is really there and I can not identify, what I checked was that taking the rows of the current Current it to give this error, now how to solve I do not know, if you are saying something the codes are zipped in the link below the dropbox, I'm following a tutorial but the code is apparently identical to the one in the tutorial: link of the tutorial

link to files in DropBox

    
asked by anonymous 15.10.2018 / 06:14

1 answer

1

Hello. The reason for this error is because it does not find the reference object and therefore causes this error. To resolve this problem first change the DataController line to a GameObject and leave it public:

 public GameObject objeto;

So when you call the object you use GetComponent < > ();

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

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

    
15.10.2018 / 11:31