Toggling multiple button states

Hello all,

I have been doing some UI research for my game and found many useful things, but I’m stumped with this problem. First, I found a great way to make buttons that are just textures from here:

And I am combining that with GUI.toggle documentation found here:

Now, what I want to do is to toggle multiple button “states” so to speak. Let’s say I have buttons A, B and C. Pressing A or B will turn on a “state”, lets call them SpeedOn and StrengthOn. These states correspond to different textures which, if they are on, should apply the appropriate texture to button C when it is clicked. Hence, if i click on SpeedOn button (button A), then click on button C, the “SpeedOn” texture will appear on button C. Instead, if I have clicked on the “StrengthOn” button (button B), the texture associated with StrengthOn should appear on button C.

There are more than 2 different states so it won’t be a simple matter like toggling a button on or off. There are at least 4 right now, but the rest are omitted for simplicity.

I hope I have explained that clearly enough. I have made some changes due to feedback and suggestions. But I am still unsure of what to put in button C’s if statement. Here is the code I have so far:

These turn on the states (buttons A and B):

if (GUI.Button(new Rect(5, 20, 52, 52), spd))
 {
 speedon = true;
 }
 
 if (GUI.Button(new Rect(5, 72, 52, 52), str))
 {
 strengthon = true;
 }

This is the code for when you click button C. Before receiving a state, it is simply an empty button:

   CheckChosen();
 if (GUI.Button(new Rect(203, 79, 20, 20), " "))
 {
   //What should go in here?
 
 }

Finally, this function checks which state is on, so it can choose the appropriate texture to put on button C:

   void CheckChosen()
 {
 if (speedon)
 texturetoapply = spd;
 if (strengthon)
 texturetoapply = str;
 if (viton)
 texturetoapply = vit;
 }

You’re having the classic paradigm thinking problem with OnGUI :slight_smile:

OnGUI gets called many times per frame with different events. The one that clicks the button isn’t the one that draws it which comes later. Put your code from your CheckChosen code outside the if body to decide on a texture and put it in a variable. Use that variable as the texture in the GUI.Button in the if which shows the third button. Set the right true or false in the if for that button.

Hi everyone,

I have figured out the answer to my own question after much fiddling around, so I thought I would share. Here is what I came up with:

TURN ON THE STATE:
if (GUI.Button(new Rect(5, 20, 52, 52), spd))
 {
 textureinsidemouse = spd;
 speedon = true;
 clickedon = "spd";
 helpmsg = true;
 }

if (GUI.Button(new Rect(314, 126, 22, 22), texturestoapply[0], mystyle))
 {
 if (Event.current.button == 1 && texturestoapply[0] != null)
 {
 Storepotential(new Vector2(1,1), 0);
 removemsg = true;
 }
 if (clickedon == null)
 return;
 if (clickedon == "upwards" || clickedon == "downwards" || clickedon == "leftwards" || clickedon == "rightwards")
 {
 Changewave(clickedon, 1, 1);
 updateAdjacentnodes(1,1);
 Clearallmouseshit();
 return; 
 }
 texturestoapply[0] = textureinsidemouse;
 gridcontent[1,1] = clickedon;
 checkAdjacentnodes(1, 1);
 Chainreaction();
 }

There’s a lot of extra stuff in there now, but basically, I made a Texture2D array and used it in the button content. At first, it is blank because there is no texture in texturestoapply[0]. Once I click on the button that turns on the state, the texture is “inside the mouse” so to speak. It will transfer to any blank button that has texturestoapply as content.

So, in the above example, spd is the name of the texture I want. When i click on the first button, it becomes textureinsidemouse. Then finally texturestoapply[0] becomes textureinsidemouse. Since the button is set to display texturestoapply[0], it will end up displaying the original spd texture. What a journey!