Reputation: 759
I have three variable
None, TeamA, TeamB
I am just doing some debugging and would like for an image to appear, depending on which is selected.
Code used to select the 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 I need to add it below the CrowdOptions, but I am not sure what the code is or how to connect it to the images.
I made UI Image box with a color; None = White, Team A = Red, Team B = Blue.
Here is the answer. I needed to turn off the 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 the GameObjects (images) were None, TeamA, TeamB
Upvotes: 1
Views: 1028
Reputation: 12631
Tim, your simplest approach would just be to "turn the game objects on and off" to make one or the other appear.
Just use SetActive
to do this - read the manual or google for literally 1000s long tutorials on this.
I don't know why I'm doing this but here's more code tips
1) Choose a random INTEGER out of three and use that,
2) DON'T call for a random every time! choose it once and use that value. this is a very basic mistake that can lead to huge bugs.
int r = Random.Range(0,3)
// means either 0,1 or 2
// READ MANUAL if you don't understand why
3) When you have to turn "one thing on" like that, a better plan is to TURN THEM ALL OFF, and then TURN ONE ON. It's a more reliable, clear way to 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 get it?
Notice how much easier it is now to, for example, add new values later, and how much easier it is to check your code is right.
Upvotes: 0