Make a button toggle between highlight and default graphics onClick?
In other words I want a button to behave like a toggle. I'm not familiar with how to do this through scripting but thought maybe it would be possible to set the button to highlighted through the OnClick option on the button.
On click button, bool is activated and the button stays highlighted until you press another button which would deselect the other option. I'm not sure the toggle would work for this since I still would want the button to make sounds upon "selected". I've disabled the mouse so I navigate by arrow keys.
Answer by DawdleDev · Apr 30, 2018 at 12:15 AM
bool isClicked = false;
public Color colNormal;
public Color colPressed;
void OnClick () {
isClicked = !isClicked;
button.GetComponentInChildren <Image> ().color = (isClicked ? colPressed : colNormal)
}
Buttons always have an image attached to their children, so you can just edit that. Also, I believe there is a way to do this in the button settings rather than in script, but I'm not sure. You're going to want to assign colNormal and colPressed in the editor or however you wish.
Hope this helps!
Ok it works somewhat, I copied in the colors I use for just moving around the buttons and clicking them. So upon clicking it I want it to chang to a brighter color. Problem is that it only seems to want the default color and then when I untoggle it, it turns darker than the default color despite the "darker" color being the color of the default button. Could it be that there is some conflict between the colors used on the normal button script and this one. I'm not sure. See video: https://www.youtube.com/watch?v=wc73vfDWjcQ
That line isClicked = !isClicked just means swap the value of isClicked. It's simpler than this, but means the same thing:
if (isClicked) {
isClicked = false;
} else {
isClicked = true;
}
The value of isClicked is what you want to check for. It says whether the button is pressed or not. The actual button component has nothing to do with this variable. However, something about the button needs to change, or it won't make sense for the user. So, the second line changes the color of the button to make it clear whether it's on or off. You can do something else too, such as change the sprite, start an animation, start a particle system, or whatever you want, but this is a quick and simple solution. Now, when you're wanting to know from another script whether the button it toggled or not, don't look in the button component. It doen't have anything that stores that variable. Ins$$anonymous$$d, check this script. For example:
//Do this:
if (GetComponent<YourScriptName> ().isClicked) {
// Do stuff!
}
//Not this:
if (GetComponent<Button> ().isClicked) {
// Do stuff!
}
Hope this clarifies a little!
Yes thanks for the clarification, makes sense now. I updated my post since that with a video, I got it working but there seems to be an issue, can you see what it is because I can't. :( https://www.youtube.com/watch?v=wc73vfDWjcQ