Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by djordr · Apr 29, 2018 at 11:09 PM · toggletoggle button

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image djordr · Apr 30, 2018 at 02:13 PM 0
Share

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

avatar image DawdleDev djordr · Apr 30, 2018 at 02:25 PM 0
Share

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!

avatar image djordr DawdleDev · Apr 30, 2018 at 02:46 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My music player is getting detached from toggle after changing scenes 0 Answers

Need help understanding toggle code. 0 Answers

Toggle Group save stats 0 Answers

Toggle input with sprite swap instead of checkmark? 4 Answers

Toggling checkbox in toggle in GUILayout.Toggle 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges