Object is null and I don't know how fix it
Well, I'll try to be as clear as possible. I'm following a tutorial (just have some basic notion about programming) and I'm stucked. There is a point where a method to move an object should be called, but it isn't. After testing the IF's of the scripts involved I apparently found the problem. There's a if that calls the movement method isn't being properly activated (comproved by debug.log):
public override void CliqueMouse(GameObject objetoAtingido, Vector3 posicaoAtingida, Jogador controlador)
{
base.CliqueMouse(objetoAtingido, posicaoAtingida, controlador);
Debug.Log("Its reaching here...");
if (jogador && jogador.humano && selecaoAtual) //The problem is here, probably in the "jogador" tests..
{
Debug.Log("It should be reaching here, but it isn't");
Debug.Log("...");
IniciarMovimento(destino); //Starts movement
}
}
My "jogador" variable is defined by the class "Jogador" below:
public class Jogador : MonoBehaviour {
public string usuario;
public bool humano;
public InterfaceHUD interfaceHUD;
public Unidades ObjetoSelecionado { get; set; }
Debug.Log("[...] The rest is empty, there is just an Initialization for the HUD varible [...]");
}
What's the properly way to test why my "jogador" variable is reaching null to the "CliqueMouse" class? I mean, is there a way to test it with Debug.log or something like that? The codes involved are much more complicated than these two parts. My "jogador" is being Initialized in another class and I don't see a way to discover where the data losts itself.
Answer by gabrimo · Jul 29, 2018 at 11:10 PM
Oh god, it was obviously a silly error of mine.
The variable "jogador" doesn't had a properly initialization on the "CliqueMouse" class parent.
jogador = transform.root.GetComponentInChildren<Jogador>();
The line above solutioned the problem.
Your answer

Follow this Question
Related Questions
Help with NullReferenceException 0 Answers
Not sure what this error is telling/asking me to do? 1 Answer
Null Reference Error 0 Answers
Null Reference Exception when reading from array of strings 0 Answers
Cannot access 2D array element 1 Answer