- Home /
Assigning Texture2D to script
I'm trying to draw a GUI with my own textures. I have a GUIObjectTemplate class in one script, and GUITab class in another script (inheriting from GUIObjectTemplate). GUITabs are instantiated and controlled in a script attached to a GameObject.
What I'm trying to do is assign Texture2D's to the "texture" and "texture_alt" variables. I do so by going to the GUITab script in the Inspector and dragging the textures into Default References. It doesn't seem to work though, they are Null at runtime and the program crashes at the GUI.DrawTexture() call : (
Below is the simplified code...
using UnityEngine;
using System.Collections;
public class GUIObjectTemplate : ScriptableObject {
public Texture2D texture;
public Rect area;
public GUIObjectTemplate() {
//Constructor
}
// Use this for initialization
public virtual void Start () {
}
// Update is called once per frame
public virtual void Update () {
}
}
using UnityEngine;
using System.Collections;
public class GUITab : GUIObjectTemplate {
public Texture2D texture_alt;
public bool selected = false;
//Constructor
public GUITab(Rect _area) {
area = _area;
Debug.Log(area); //OK!
Debug.Log(texture_alt); //Returns Null :/
}
public void Draw() {
Debug.Log(area); //OK!
Debug.Log(texture_alt); //Null
GUI.DrawTexture(area, texture_alt);
}
}
Answer by simonmc · Jul 29, 2011 at 10:38 PM
I have the same issue. For me, the default references work in the editor but not on a standalone build.
add some more parameters to your constructor and pass in the textures wherever you instantiate the objects.
Your answer
Follow this Question
Related Questions
Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers
"null texture passed" for any texture2D other than the 2 originals. 1 Answer
Default string value... empty? (C#) 2 Answers
Public variables null after assigned in the inspector. 3 Answers
C# Inheritance, base class attributes, override and null object 1 Answer