- Home /
Null reference exception[SOLVED]
Hi everyone, i had an error:
"NullReferenceException: Object reference not set to an instance of an object"
I was trying to put an image to a button, like this:
public Texture[] buttonTxt = new Texture[10];
void OnGUI() {
if(createButtonT(50, 50, 100, 30, buttonTxt[0], style)) {
}
}
he throws a exception as you can see, but i've looking for the answer for my question and i didn't found a case like the mine. My cretaButtonT() is a method that creates a button with a texture, seated on other script:
public bool createButtonT(float left, float top, float width, float height, Texture content, GUIStyle style) {
return GUI.Button(new Rect((screenX-screenX)+left, (screenY-screenY)+top, width, height), content, style);
}
Thanks for who will help me =)
We have no idea where your NullReferenceException is co$$anonymous$$g from, can you show us which line its referring to?
With what you've given, my vague guess is that buttonTxt[0]
is null because you have not initialized it. the line public Texture[] buttonTxt = new Texture[10];
creates an array, but it does not fill it with valid content.
how can i fill with valid content? I've filled in inspector of the Unity editor. Well the null reference entire error is here:
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.Button (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUI.cs:248) UnityEngine.GUI.Button (Rect position, UnityEngine.Texture image, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUI.cs:225) mainClass.createButtonT (Single left, Single top, Single width, Single height, UnityEngine.Texture content, UnityEngine.GUIStyle style) (at Assets/_gameAssets/_scripts/mainClass.cs:53) clarinete.OnGUI () (at Assets/_gameAssets/_scripts/clarinete.cs:28)
i'd solved, the problem was on the declaration of an GUIStyle, where the GUIStyle has to be public!