- Home /
Change texture of GUItexture from script
hi, how to change texture of GUItexture with the name of the texture?
something like this..
var texturename="casualtexture";
GameObject.Find(photo1).guiTexture.texture=texturename;
Accessing resources with their names alone is a bad idea in general. Why is it so necessary in this case? You should be able to find an object by its path given that it is in the Resources folder. Otherwise, you should either use a Dictionary of pairs linking textures with names (and then index that using the string), or just have a bunch of Texture variables that you can assign in the inspector.
is necessary because I take the names from an xml file..
Answer by NorthernEagle · May 18, 2012 at 09:07 PM
You can also assign Texture2D link as public variables and assign GuiTexture like that. For example:
( C# code follows )
public GuiTexture clickButton;
public Texture2D clickButtonTexture;
clickButton.guiTexture.texture = clickButtonTexture;
I'm co$$anonymous$$g in late, but I would like to say that your solution was exactly what I was looking for, thanks.
Answer by gianni123 · May 12, 2012 at 08:57 AM
same problem for audiosource..
var audioname="casualsound";
GameObject.Find(audioplay).GetComponent("AudioSource").AudioClip=audioname;
Answer by gianni123 · May 12, 2012 at 10:02 AM
SOLVED
I created the folder resources in assets
I put my image files inside..
var texturenameok=Resources.Load(texturename); GameObject.Find(photo1).guiTexture.texture=texture nameok;
If this worked for you, you should probably accept this answer.
Answer by LSPressWorks · Jul 25, 2014 at 05:00 PM
HA! I think this post just solved like, 3 problems for me! I had no idea UNITY did resources!
Your answer
Follow this Question
Related Questions
Changing Object Texture When button is pressed. 3 Answers
Changing GameObject texture? 4 Answers
Changing texture for pause button 1 Answer
script help gameobject to playercar 1 Answer
referring to a particular material 0 Answers