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 /
avatar image
1
Question by WavyRancheros · Aug 20, 2015 at 02:04 PM · togglesource

Get source of onValueChanged Event

Hi,

I have several Toggles.

I have a Controller class that has one OnValueChanged(bool) method. I want that all toggles call that one method and let the controller decide what to do.

Problem is that there seems to be no way to know where the OnValueChanged event is coming from.

Other UI implementations always have a OnEvent(source, value) implementation.

Do I have to create one OnValueChanged method per Toggle, or are there other, better solutions?

Comment
Add comment · Show 4
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 Nerevar · Aug 20, 2015 at 02:29 PM 0
Share

You can do that if you assign the function to call on a OnValueChanged event from the inspector referencing the function from you Controller class.

$$anonymous$$aybe you can use a ToggleGroup to handler your toggles? Or tell us more about what behaviour you expect or what you are trying to achieve.

(Recently I had to work with several toggle groups and change the color of the parent objects if some of the children toggles where selected)

avatar image WavyRancheros · Aug 20, 2015 at 02:38 PM 0
Share

It's a pretty easy setup. I have 4 Toggles that toggle the visibility of objects. It's supposed to be extensible with more toggles and objects added over time.

So I wanted to have 1 OnValueChanged(Toggle source, bool value) method that does:

 public void OnValueChanged(Toggle source, bool value)
 {
    if(source == toggle1)
    {
       gameobject1.render.enabled = value;
    } else if (source == toggle 2)
    {
       gameobject2.render.enabled = value;
    }
    ....
 }

Or, a bit cleaner, access a Dictionary < Toggle, GameObject >:

 public void OnValueChanged(Toggle source, bool value)
 {
     dictionary[source].renderer.enabled = value;
 }

avatar image Nerevar · Aug 20, 2015 at 03:02 PM 1
Share

Well you can do :

 public void OnValueChanged(Toggle source)
  {
      
      dictionary[source].renderer.enabled = source.isOn;
  }

then you can assign from the inspector the toggle reference for you callback.

edit: should look like this alt text I don't know if there is another way...

capture.png (6.0 kB)
avatar image WavyRancheros · Aug 25, 2015 at 06:48 AM 0
Share

Thank you!

0 Replies

· Add your reply
  • Sort: 

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

25 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

Related Questions

Sound getting deeper 2 Answers

client/server in the same project? 0 Answers

InvalidCastException: Cannot cast from source type to destination type. 3 Answers

Toggling through music 1 Answer

Is there a toggle for snap to grid? 10 Answers


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