Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Shivam_Dhoot · Sep 30, 2014 at 10:13 AM · c#togglelisteners

Adding a listener to Toggle.onValueChanged via Script

Hello,

I was trying to add a listener on a Toggle Button but i am not able to write that. Can anybody please help me. Here is what i am doing

 Toggle sound = GameObject.Find("soundOnOff").GetComponent();
 sound.onValueChanged.AddListener(gameSoundStatusChanged); // --- Error

and the function is

 public void gameSoundStatusChanged()
 {....}

it says .addlistener has some invalid parameters. Cause it asks for parameter of type UnityAction function. Please help me on how to write the function in given syntax.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by batteryyrettab · Oct 24, 2014 at 07:20 AM

I just had the same issue. You just need to pass a float parameter into your method. eg.

     public Slider myMusicVolumeSlider;
     AudioSource myBGMusic;
 
     void Start () 
     {
         myBGMusic = GetComponent<AudioSource>();
         myMusicVolumeSlider.value = 0.2f;
         myMusicVolumeSlider.onValueChanged.AddListener(RaiseVolume);
     }
 
     void Update () 
     {
         //I Don't want to run this every frame....
         //myBGMusic.volume = myMusicVolumeSlider.value;
     }
 
     public void RaiseVolume(float value)
     {
         myBGMusic.volume = myMusicVolumeSlider.value;
         //Or even just... myBGMusic.volume = value;
     }

Comment
Add comment · Show 2 · 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 tmendez · Mar 15, 2017 at 07:46 AM 0
Share

Im not sure how this works with a Toggle like in the original post. What would the listener have to accept? A callback?

avatar image batteryyrettab tmendez · Jun 08, 2018 at 01:17 AM 0
Share

See the answer Elvira_Z provided. It is the more correct answer to your question. I provided one for a Slider rather than a Toggle component but the principle is the same.

avatar image
0

Answer by Elvira_Z · Apr 29, 2016 at 08:39 AM

Only just like this:

Toggle sound = GameObject.Find("soundOnOff").GetComponent(); sound.onValueChanged.AddListener(gameSoundStatusChanged);

public void gameSoundStatusChanged(bool isclick) {....}

Comment
Add comment · 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
0

Answer by Ignorant · Jun 11, 2018 at 03:53 PM

your method gameStatusChanged() needs to take bool param so change it to: gameStatusChanged(bool newValue) and it will work fine

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Toggle between scripts with a script? 2 Answers

[C#]Toggle Run on/off 2 Answers

Distribute terrain in zones 3 Answers

C# Make array based on script variable 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