Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by yijsw · Aug 26, 2019 at 04:42 AM · delegatelistener

How to add a function from another class to a Slider's OnValueChanged with AddListener()?

Hello, I'm new to Unity/C# and need quite a bit of help. In my program, I have one UI slider at start and a bunch of prefabs instantiated during runtime. Each prefab has a script that changes its localScale. I want the user to click on a prefab, and the slider's OnValueChanged list of functions to update so it only changes the dynamic variable of the prefab the user clicked on. The only problem is that the last line of StartScaleMode(), where I use AddListener(), does not seem to work.

The function for adding a function to OnValueChanged :

     public void StartScaleMode()
     {
         GameObject ScaleSlider = GameObject.Find("ScaleSlider");
         Slider SlideScript = ScaleSlider.GetComponent<Slider>();
         Scale script = hit.collider.gameObject.AddComponent<Scale> ();
         SlideScript.onValueChanged.AddListener(delegate 
         {
              script.AdjustSize(SlideScript.value); 
         });
     }

And the other class, which is attached to the prefabs, is:

    public class Scale : MonoBehaviour {
          public void AdjustSize(float newSize)
          {
           gameObject.transform.localScale += new Vector3(newSize, newSize, newSize);
          }


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
1
Best Answer

Answer by Hellium · Aug 26, 2019 at 05:28 AM

onValueChanged is an event taking a float as parameter, so I believe you can do:

 SlideScript.onValueChanged.AddListener(script.AdjustSize);

Also, I advise you to remove the event listeners beforehand, if you don't do it already.

 SlideScript.onValueChanged.RemoveAllListeners();
Comment
Add comment · Show 4 · 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 yijsw · Aug 26, 2019 at 08:14 PM 0
Share

Thanks for the help. Unfortunately, when I go to the inspector during runtime, no functions seem to have been added to OnValueChanged event.

avatar image Hellium yijsw · Aug 26, 2019 at 08:37 PM 0
Share

This is the expected behaviour, callbacks added at runtime won't appear in the inspector.

avatar image yijsw Hellium · Aug 26, 2019 at 08:49 PM 0
Share

Ah I see. When I change the value of the slider, though, the size of the object does not change. I've checked the Physics.Raycast and it returns the correct collider. Any possible ideas as to why or how to find the issue? You have helped me so much already, so don't feel pressured if you have no idea.

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

109 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

Related Questions

Using Listeners for Sequences and Different Classes 1 Answer

Button Listener Takes Reference But I Want to Pass Value? 1 Answer

UI toggle.onValueChanged assigning method via script 1 Answer

Split Screen Game Audio Listeners 5 Answers

Unity 4.6 - Editor Scripting - Add Reflection Method as a Persistent Listener 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