- Home /
Change object texture using String [Java]
Hey all!
This question may have been answered before but it was never what I could implement into my script.
Basically I have a string which I have stored as NULL. Then I have 2 textures BlankIcon and WoodIcon. When the string is changed from NULL to either BlankIcon or WoodIcon then it will change another variable called TheNewItem's texture to either the BlankIcon texture or the WoodIcon texture. Here's my script so far;
//String
var NewItemName = "NULL";
//Icons
var BlankIcon : Texture;
var WoodIcon : Texture;
var TheNewItem : Texture;
function Update()
{
if (NewItemName != "NULL")
{
TheNewItem = NewItemName; //This is the line I am unsure about.
}
}
That string I want to represent a variable name, so whatever the string is called if its a variable then it will assign it. I know you can do this with an If Statement but with the amount of items I will have this could be a very long script is there any easier way or do I just have to use if statements? Thank you for reading this! :)
Slayer, if you check the scripting docs for $$anonymous$$aterial, there are some examples http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial-mainTexture.html So do like if(NewItemName == "wood") { renderer.material.mainTexture = WoodIcon; } or something like that. However, don't do it in the Update function, only do it when the texture needs to change.
That's what I was going to use but with the mass amount of textures I am going to be using I didn't want to have a whole script full of if statements but thank you! I will use that way then :)
Hi Slayer, Why can't you create a String array and store the Texture name and load according to your use?
sorry for the late reply! was at college all day, I know how to create the string array
var Textures = String [];
but how do I load them accordingly?
Answer by Unity_gamer · Aug 22, 2012 at 06:43 AM
Add the String Names in the Editor(Make sure your Texture Name and the Given String name are Same). For Example If you want to load the Textures Randomly,
var Random_numbr : int;
Random_numbr = Random.Range(0,49);(If you have 50 Textures)
Make sure the Texture are in the Resource Folder and Use Resources.Load(Textures[Random_numbr]).(Texture[Random_number] holds String Name)
Hope this will Help you..:)
Thanks..