- Home /
Why does one GUI change affect every GUI on scene? How can I fix that?
I have this chunk of code:
if(mainCam.transform.position == new Vector3(1866, 1, -72.4901f)) {
GUI.contentColor = Color.red;
GUI.Label (new Rect(20,20,170,30), "Selecting a stripe is required");
//stripe 1 toggle
toggleTxt = GUI.Toggle(new Rect(300, 85, 100, 30), toggleTxt, "");
toggleTxt = GUI.Toggle(new Rect(300, 140, 100, 30), toggleTxt, "");
toggleTxt = GUI.Toggle(new Rect(300, 185, 100, 30), toggleTxt, "");
toggleTxt = GUI.Toggle(new Rect(300, 230, 100, 30), toggleTxt, "");
toggleTxt = GUI.Toggle(new Rect(300, 275, 100, 30), toggleTxt, "");
toggleTxt = GUI.Toggle(new Rect(300, 335, 100, 30), toggleTxt, "");
//stripe 7
toggleTxt = GUI.Toggle(new Rect(300, 380, 100, 30), toggleTxt, "");
if(GUI.Button (new Rect (5,Screen.height-40,170,30), "Back")) {
print ("You clicked the button!");
mainCam.transform.position= new Vector3(0, 1, -10);
Camera.main.orthographicSize = 500f;
}
}
I only one the text portion that says "Selecting a stripe is required" in red but I'm not sure how to individualize it for one label? When I change the color of the label, it changes the color of the text for all text on the screen. So now all my labels are red when I only want one label to be red.
Also, I noticed the same thing happen for GUI.toggle feature. I created 7 toggles so that they can be next to a game object for "selecting". However, I want all the toggles to be tied together, so that only one toggle can be selected at a time. Right now, if I select one toggle, all the other toggles are selected as true. If I don't select a toggle, all the other toggles are unselected. I only want to be change to move down the toggles and have them turn on and off (but only one selection in this grid of toggles).
It seems like this problem happens with all the GUI elements. How do I isolate the commands?
Thanks in advance!
This is all written in C#. If a solution is in JS, I can try and translate it into C# :)
EDIT: I figured out how to do the toggle from this post: http://answers.unity3d.com/questions/129813/using-guitoggle-to-onoff-buttons.html
Answer by robertbu · Feb 12, 2014 at 08:07 PM
How about saving and restoring the color:
Color savedColor = GUI.contentColor;
GUI.contentColor = Color.red;
GUI.Label (new Rect(20,20,170,30), "Selecting a stripe is required");
GUI.contentColor = savedColor;
Cool. I assume the same thing can be done for toggle? What is done differently if I save and restore the color? How does that different from what I did previously? Just trying to understand the meaning and logic behind it. Thank you so much robertbu! :D
As you progress from one GUI statement to the next, each statement uses the GUI.contentColor. By saving and restoring the color, we make the GUI.contentColor for the first GUI.Toggle() is the same as it would have been if the color had not been changed at all.
Your answer
Follow this Question
Related Questions
How does GUI.Toggle work? 2 Answers
Pause Button Problems 2 Answers
Close my GUI button by repressing the same Hot-key. 3 Answers
Gui toggle active inactive states 3 Answers