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 RCAVelez · Jan 12, 2018 at 12:42 AM · slider

How to change object reference in slider under OnValueChanged during runtime with c# script? Not through editor

I know how to find the slider itself but i don't know how to change that thing the arrow is pointing to during run time with a script using c#. I want it to reference to an object but not through the inspector, through script. This is the closest i can get but am stuck.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;

 public class Block_Change_Behavior : MonoBehaviour {
 public GameObject slider;
 void Start () {

     slider = GameObject.FindWithTag ("slider");

 }
 OnMouseDown() {
    slider.GetComponent<Slider> ().onValueChanged.AddListener(this);![alt text][1]
 }
 

alt text

[1]: /storage/temp/109111-screen-shot-2018-01-11-at-63806-pm.png

screen-shot-2018-01-11-at-62333-pm.png (83.8 kB)
screen-shot-2018-01-11-at-63806-pm.png (152.0 kB)
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
3
Best Answer

Answer by Brogan89 · Jan 12, 2018 at 02:45 AM

Adding a gameobject in the inspector is purely only to list a bunch of functions that can be accessed on that gameobject. You don't need to do that in code. With the .AddListener() function you only need to pass in the function that you want to be called when the slider value has changed.

Because the UnityAction in this case also passes a float as a parameter so the callback function needs to also have that parameter. .AddListener(DoSomething);

 void DoSomething(float sliderValue)
 {
      // do stuff here
 }


If you dont want it to have the parameter you can use lamda expressions .AddListener((s) => DoSomething());

Its also worth noting that this callback function can be anywhere, even from another script. So you could do something like .AddListener(otherScript.DoSomething)

Also you shouldn't add a listener every time you press the mouse button down... you should only add your listeners once. In the Start() generally.

You're script should look something like this:

 public class Block_Change_Behavior : MonoBehaviour 
 {
     public Slider slider;
 
     void Start()
     {
         slider.onValueChanged.AddListener(OnSliderValueChanged);
     }
 
     void OnSliderValueChanged(float sliderValue)
     {
         Debug.Log(sliderValue);
     }

}

Comment
Add comment · Show 1 · 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 RCAVelez · Jan 12, 2018 at 02:59 AM 0
Share

Thank you for the awesome explanation!

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

76 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

Related Questions

how to create GUI slide effect ?? 1 Answer

Having trouble positiong UI sliders using OnGui 1 Answer

how to make a Horizontal slider & toggle in C# with GUI textures 1 Answer

Save changes in Options scene 1 Answer

Unity 4.5.2p2 SliderJoint2D broken or different? 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