I want to use static variables to provide Gui-text's to other scripts. In the script where I declare those variables as static everything works fine- in all other scripts I get a NullReferenceException
.
Here the code of the Outputscript:
public class OutputScript : MonoBehaviour {
public static UnityEngine.UI.Text CommandText;
public UnityEngine.UI.Text ct1;
void Start()
{
CommandText = ct1;
Debug.Log("static output's set");
}
}
Here is a method of another script which accesses it:
void OnTriggerExit()
{
OutputScript.CommandText.text = "";//nullRefException
}
If I try this in the OutputScript, there's no exception
void Start()
{
CommandText = ct1;
OutputScript.CommandText.text = "";//works fine
Debug.Log("static output's set");
}
If I check in the other scripts for null
it returns true
.
I know what a NullReferenceException is, but in this case I've set the public static variable, but its value is only not null in this script which isn't logical for me. I really have no idea what's going on there.
For people who don't know Unity3d Scipt enigne:
- the Start() method gets called always first
- the OnTriggerExit() method gets executed after Start()