- Home /
Setting guiTexture.texture gives NullReferenceException
With the following code:
public Texture2D Tick;
public GUITexture Test;
void Start () {
Tick = (Texture2D) GameObject.Find("Tick").guiTexture.texture;
print(Tick);
Test = new GUITexture();
Test.texture = Tick;
}
I get the following output:
tick_mark (UnityEngine.Texture2D)
NullReferenceException
HUD.Start () (at Assets/Scripts/Player/HUD.cs:46)
Line 46 points to: "Test.texture = Tick;"
Obviously, as shown by the print statement, Tick definitely holds a valid Texture2D object.
Casting it explicitly using (Texture) or (Texture2D) has no effect, as does using Test.guiTexture.texture , or a combination of them.
I am very much stumped
Answer by AmoralAckbar · Jun 10, 2013 at 03:53 PM
A NullReferenceException gets thrown when you try to access a member on a null object. So my guess is that the problem is with Test, not with Tick. I believe that GuiTexture is a component not a game object, so doing what you are doing isn't allowed. Here is a thread that I think will get you what you need:
http://forum.unity3d.com/threads/18321-GUITexture-from-script
Your answer
Follow this Question
Related Questions
How to fix it? 0 Answers
Toggle Only Works Once 1 Answer
Working around Texture2D.Apply() performance issue 0 Answers
GUI Button not displaying an icon 3 Answers
GUI.DrawTexture() into a Layer? 1 Answer