- Home /
Assigning Texture via C# Script
Hello, I am making a simple Japanese character matching game. I have an empty GUITexture in my scene and I would like it to change based on a random number. The files are all in my Assets\Resources folder and named 0.png all the way to 50.png. So if the random number is 37, 37.png gets loaded as the texture of the GUITexture object.
I have a Texture variable called tex and the pic object is tied to the GUITexture object in my scene. I have tried using textures, texture2d, guitexture and none of them seem to work. I don't get any errors but the texture never changes (I have the same line that assigns the texture in my OnGui() for testing purposes).
I am at my wits end with this. I am sure it is something simple but my brain is fried at this point. Thanks in advance.
public int pick()
{
picked = Random.Range(0, 50);
tex = (Texture)Resources.Load(picked);
pic.texture = tex;
Debug.Log(picked);
return picked;
}
UPDATE: For the heck of it, I added a cube to the scene and the texture assignment is working on that but it will not work on a GUITexture for some reason.
What is the pic class and how is it tied to the GUITexture? Can you show the code that you are using to display the texture? I assume you've checked tex to make sure it is not null?
pic is a public GUITexture that has the GUITexture object that I want to change assigned to it in the inspector.
tex starts out as null when it is created but doing Debug.Log(tex) shows the value of what is in there. Not sure how I would check to make sure it is actually loading the file though. Sorry
The texture is displayed on screen at all times, there is no code associated with it other than trying to change the texture. I have tried assigning a texture in the inspector to make sure that it is displaying properly (it does), just doesn't change.
Hopefully this clears some things up.
Answer by robertbu · Apr 01, 2013 at 01:41 AM
I'm not sure how you have your linkages setup. Below is your code with a bit of framework so I could test it and make sure it would work:
using UnityEngine;
using System.Collections;
public class GT : MonoBehaviour {
public GUITexture pic;
Texture tex;
int picked;
void Update() {
if (Input.GetKeyDown (KeyCode.Space))
Pick ();
}
public int Pick() {
picked = Random.Range(0, 12);
tex = (Texture)Resources.Load(picked.ToString());
if (tex != null)
pic.texture = tex;
else
Debug.Log ("Failed to load image");
Debug.Log(picked);
return picked;
}
}
The way I used this code was to:
I attached this script to an empty game object.
I created a GUITexture by using GameObject/CreateOther/GUITexture
I dragged and dropped the GUITexture object onto the pic variable to created the linkage.
Everything worked fine. I'm guessing there is a problem with how you are creating or initializing your 'pic' GUITexture. This is not necessarily how I would setup things. I'd probably attach the script to the GUITexture game object and use GetComponent() to initialize pic. Also note that the code as posted would not compile because picked is a number and Resources.Load() takes a string.
Thanks a lot. I am not really sure where the problem was but I started almost from scratch and using a bit of the code you posted and it worked. Thanks again.