I have been trying to get the GUI texture on unity working (more specifically, changing the image), but every time I try to do so, it either does not want to work or it un-references itself (becoming “none” in the inspector). This also happens when I try to just drag and drop the reference into the inspector, but when I run it and press the input for it to work, it un-references itself as well, could it be something wrong with the logic? Here is what I have so far:
public class HUDTest : MonoBehaviour
{
public GUITexture livesImage;
public GUITexture tempTex;
public int lives = 3;
void Start()
{
livesImage = GameObject.Find("TestImage01").GetComponent<GUITexture>();
}
void Update()
{
//A simple test
if(Input.GetKeyDown("0"))
{
lives++;
livesImage = getNumberTexture(lives);
}
}
GUITexture getNumberTexture (int number)
{
//Depending on the number recived...
switch (number)
{
case 0:
//If number 0 is selected, get the GUItexture 0
tempTex = (GUITexture)Resources.Load("Num0", typeof(GUITexture));
break;
case 1:
//If number 1 is selected, get the GUItexture 1
tempTex = (GUITexture)Resources.Load("Num1", typeof(GUITexture));
break;
case 2:
//If number 2 is selected, get the GUItexture 2
tempTex = (GUITexture)Resources.Load("Num2", typeof(GUITexture));
break;
case 3:
//If number 3 is selected, get the GUItexture 3
tempTex = (GUITexture)Resources.Load("Num3", typeof(GUITexture));
break;
case 4:
//If number 4 is selected, get the GUItexture 4
tempTex = (GUITexture)Resources.Load("Num4", typeof(GUITexture));
break;
case 5:
//If number 5 is selected, get the GUItexture 5
tempTex = (GUITexture)Resources.Load("Num5", typeof(GUITexture));
break;
case 6:
//If number 6 is selected, get the GUItexture 6
tempTex = (GUITexture)Resources.Load("Num6", typeof(GUITexture));
break;
case 7:
//If number 7 is selected, get the GUItexture 7
tempTex = (GUITexture)Resources.Load("Num7", typeof(GUITexture));
break;
case 8:
//If number 8 is selected, get the GUItexture 8
tempTex = (GUITexture)Resources.Load("Num8", typeof(GUITexture));
break;
case 9:
//If number 9 is selected, get the GUItexture 9
tempTex = (GUITexture)Resources.Load("Num9", typeof(GUITexture));
break;
default:
//If it is not a number, return a "0"
tempTex = (GUITexture)Resources.Load("Num0", typeof(GUITexture));
break;
}
return tempTex;
}
}
This is a lives example I written up, all it is supposed to do is change a GUITexture when the lives number changes. What am I doing wrong and what can I do to fix this problem?