user interface - How to have an image show up if a condition is met in Unity -
i have 3 variable
none, teama, teamb
i doing debugging , image appear, depending on selected.
code used select crowd.
public enum crowdoptions {none, teama, teamb}; public static crowdoptions crowdoptions; if(random.value < .33){ crowdoptions = crowdoptions.none; } else if (random.value > .66){ crowdoptions = crowdoptions.teama; } else { crowdoptions = crowdoptions.teamb; }
i know need add below crowdoptions, not sure code or how connect images.
i made ui image box color; none = white, team = red, team b = blue.
here answer. needed turn off game object:
if(random.value < .33){ crowdoptions = crowdoptions.none; gameobject.find("none").getcomponent<image>().enabled =true; gameobject.find("teama").getcomponent<image>().enabled =false; gameobject.find("teamb").getcomponent<image>().enabled =false; } else if (random.value > .66){ crowdoptions = crowdoptions.teama; gameobject.find("none").getcomponent<image>().enabled =false; gameobject.find("teama").getcomponent<image>().enabled =true; gameobject.find("teamb").getcomponent<image>().enabled =false; } else { crowdoptions = crowdoptions.teamb; gameobject.find("teamnone").getcomponent<image>().enabled =false; gameobject.find("teama").getcomponent<image>().enabled =false; gameobject.find("teamb").getcomponent<image>().enabled =true; }
then name of gameobjects (images) none, teama, teamb
tim, simplest approach "turn game objects on , off" make 1 or other appear.
just use setactive
- read manual or google literally 1000s long tutorials on this.
i don't know why i'm doing here's more code tips
1) choose random integer out of 3 , use that,
2) don't call random every time! choose once , use value. basic mistake can lead huge bugs.
int r = random.range(0,3) // means either 0,1 or 2 // read manual if don't understand why
3) when have turn "one thing on" that, better plan turn them off, , turn 1 on. it's more reliable, clear way program.
void turnalloff() { teama ... disabled teamb ... disabled teamc ... disabled } void turnononerandomitem() { int r = random.range(0,2) turnalloff() if ( r == 0 ) teama .. enabled if ( r == 1 ) teamb .. enabled if ( r == 2 ) teamc .. enabled }
you it?
notice how easier to, example, add new values later, , how easier check code right.
Comments
Post a Comment