Hey all,
I want to have a texture come up next to an object. I’ve made an empty game object, childed it to the GameObject, then attached a script that declares a GUITexture (called guiTex) and a Texture2D (called textImage). I then try to declare the guiTex as a new GUITexture, then set its texture to textImage. In running the code below, it’s become apparent that by the time I get to ‘guiTex.texture = textImage;’, the guiTex object is still null. I can’t figure out why this is the case.
Code below:
using UnityEngine;
using System.Collections;
public class HoverUI : MonoBehaviour {
GUITexture guiTex;
public Texture2D textImage;
void Awake()
{
}
// Use this for initialization
void Start () {
guiTex = new GUITexture ();
Debug.Log (textImage);
Debug.Log (guiTex);
//guiTex = guiTexture;
guiTex.texture = textImage;
setMyPlace ();
}
// Update is called once per frame
void Update () {
}
void setMyPlace()
{
guiTex.transform.localPosition = new Vector3 (100, 100, 0);
}
}
Eventually I’m gonna have to change ‘new Vector 3’ to something related to worldToScreenSpace, but I’m not worried about that now, I’m just trying to get the declaration to work.
When I run this code, I get 3 things:
The game finds the reference to the texture2D (it returns the texture’s name)
The game returns ‘null’ where I ask for debug.log(guiText)
finally, this error comes up:
NullReferenceException
HoverUI.Start () (at Assets/Scripts/Pieces/UI/HoverUI.cs:18) (18 is where the ‘guiTex.texture = textImage;’ is)
I feel like I’m missing something super simple, but I don’t know what it is. I changed ‘Start()’ to ‘OnGUI’ and this didn’t do anything, so I’m not sure what else to do.
Thanks in advance for the help!