- Home /
The question is answered, right answer was accepted
string plus int equals the name of the variable? How?
I have 6 clothing textures.
public Texture2D clothe1;
public Texture2D clothe2;
public Texture2D clothe3;
public Texture2D clothe4;
public Texture2D clothe5;
public Texture2D clothe6;
I wish to assign one of them to a material. It will be done by checking a player prefs int variable.
The variable is assigned to an int variable called "Top"
So I basically want to say something like
gameObject.renderer.material.mainTexture = "clothe" + Top.ToString();
emphasis on ""clothe" + Top.ToString();"
because then it equals clotheWHATEVERTHENUMBERIS
which would equal one of the textures..
But its not working. How would I achieve this?
Answer by thornekey · May 31, 2014 at 02:43 PM
nevermind, i solved it like this:
if(Top == 1) {
gameObject.renderer.material.mainTexture = clothe1;
} else
if(Top == 2) {
gameObject.renderer.material.mainTexture = clothe2;
I knew you would do it this way, so I suggested a Switch Statement :D
switch (Top)
{
case 1:
gameObject.renderer.material.mainTexture = clothe1;
break;
case 2:
gameObject.renderer.material.mainTexture = clothe2;
break;
default:
gameObject.renderer.material.mainTexture = clothe1;
break;
}
Avoid 6 deep nested ifs!
Sometimes answering on unity answers is so frustrating. I'll give you a thumbs up for cold blood.
Answer by meat5000 · May 31, 2014 at 01:55 PM
I know it works in JS, not so sure about C#...
I use .ToString()
JS Example
clone = Instantiate(nextBlockObject, newPlacement, Quaternion.Euler(0,0,turnAngle*nextPos));
cloneCount += 1;
clone.name = ("clone-"+nextBlockObject.name.ToString()+cloneCount.ToString());
Cannot implicitly convert type string' to
UnityEngine.Texture'
gameObject.renderer.material.mainTexture = ("clothe" + Top.ToString());
Top.ToString() is equal to 1
so it reads clothe1
which is
public Texture2D clothe1;
but it doesnt like it being a string when the actual variable is a texture.
The whole reason is because Top can change up to 6 and there are 6 clothe1-6 variables...
@meat5000 ToString in JS and C# bot work pretty much the same. Problem is he is assigning string directly to texture property of material, ins$$anonymous$$d of actually finding his the texture with that name first, and then assigning it.
Answer by AlbertGp82 · May 31, 2014 at 02:20 PM
if i understand ... you cant create a name variable with string Have many options to do that, dictyonari,hashtable,Enums. or something like that can be more powerful and elegant but i can ,hope, get u an easy form.
mmmhhhh..
public Texture2D clothe1;
public Texture2D clothe2;
public Texture2D clothe3;
public Texture2D targetClothe;
public void ChangeMainTexture(){
int myTop = PlayerPrefs.GetInt("myTop")
switch(myTop){
case 1 :
targetClothe = clothe1;
break;
case 2 :
targetClothe = clothe2;
break;
case 3 :
targetClothe = clothe3;
break;
}
gameObject.renderer.material.mainTexture = targetClothe;
}
Can use string in switch if u want, or use Resources.Load() for load with string match, somethin like that
gameObject.renderer.material.mainTexture = Resources.Load("/materials/textures/clothe" + Top,Texture);
Take a read about Resources folder and his access.This method its good idea if you do before gameplay,because have a heavy memory/procces impact,the other referencing in variables its better idea,ever you can, if you do during gameplay
Can be syntax error in myTop declaration, dont tried,im not a programmer C# :).Anyway you can explore more options to do that, you have many many ways.
Hope this works. Sorry for my english, its pretty pretty low cheers
I dont understand how to implement that into my full code..
Heres a link if you would $$anonymous$$d taking a look
i dont know the method networkView.RPC(),i never take a look at networks. But if u give a int value to getTop()
public void getTop (int Top) {
switch(Top){
case 1 :
targetClothe = clothe1;
break;
case 2 :
targetClothe = clothe2;
break;
case 3 :
targetClothe = clothe3;
break;
}
gameObject.renderer.material.mainTexture = targetClothe;
}
or can do ...
public void getTop (int Top) {
gameObject.renderer.material.mainTexture = Resources.Load("/materials/textures/clothe" + Top,Texture);
}
i dont remember if its -Texture or -Texture2D in Load().This is you have the textures in "local" folder in your assets project like Resources/material/textures, sure u can do that if u want take the texture of www, but i dont know exactly how,take a look unity help.
Follow this Question
Related Questions
How can i save a int for multiple texture. Please Read. 0 Answers
variable with int value not passing to command 1 Answer
Playerprefs across Scenes 1 Answer
Public Texture Variables Not Working 0 Answers
Accessing a Variable From Another Script 2 Answers