Anyone had a situation like this? I’m after upgrade to unity 5.4. I continue work on my project and something strange is going on. In script I declare public GameObject, the field shows up in inspector, I drag&drop object… Everything is fine as usual.
But when I run the script declared GameObject is null. In inspector it looks ok. In other, older scripts(made in 5.2.3) everything is ok. What could be the cause?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PremBlockScript : MonoBehaviour {
private int PremGem;
public Text PremGemText;
public void Start()
{
PremGemInit();
}
void PremGemInit()
{
PremGem = PlayerPrefs.GetInt("PremGem");
PremGemText.text = PremGem.ToString();
}
}
It returns nullReferenceException on PremGemText; In inspector Text field is still visible.
But when I try folowing and put PremGemText on Start it works fine…
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PremBlockScript : MonoBehaviour {
private int PremGem;
public Text PremGemText;
public void Start()
{
PremGemInit();
PremGemText.text = PremGem.ToString();
}
void PremGemInit()
{
PremGem = PlayerPrefs.GetInt("PremGem");
}
}
should I go to sleep, because I don’t think anymore??
XXXXXXXXXXXXXXXXXX update
OK, so my apologies to Unity Team It was my mistake, not the updated issue…
I have declared this PremBlock class in other script as ‘new’; of course I shouldn’t do this because it’s a monobehavior. Anyway Unity shows it as yellow warning not as red error so I missed that fact.
When object with script started, PremGemText.text was working as in inspector from Start(), but later it was called from other script with wrong declaration and it probably caused object from inspector turned to null. At least this is how I understand it…