Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Mol · Apr 03, 2015 at 12:26 PM · uireferencesliderlistener

Pass in a reference directly into a script so that it update the appropriate variable value when the slider is moved.

I was wondering if there is a way to pass in a reference directly into a script so that it update the appropriate variable value when the slider is moved. I need to use a lot of slider to change scripts floats. This two methods works but it's dirty to code a function for each slider, so i look for a generic approach.

 //Molecule Script
 public static float generateThreshold= 1.0f;
 //GUI Script
 public Slider treshold;
 
 public void SliderUpdate (){
         Molecule.generateThreshold = treshold.value;
         }
 //OR
 public void SliderUpdate (float val){
         Molecule.generateThreshold = val;
     }
 

By the way i tried to use the generic {get; set;} methods it's work fine with dynamic float but mine are statics. So i thought about using ref key word (https://msdn.microsoft.com/en-us/library/14akc2c7.aspx) But onValueChanged take float argument so my code below don't work but it's the kind of method i want.

 void Start(){
                 treshold.onValueChanged.AddListener (SliderUpdate (ref test,treshold));
             }
         
     public void SliderUpdate (ref float floatToUpdate,Slider sliderToLink){
         floatToUpdate = sliderToLink.value;
         }

Thanks

Please excuse my poor English

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 gjf · Apr 03, 2015 at 09:32 AM 0
Share

if you're using the same script for multiple sliders then don't use static variables - get a reference to each instance to access each of them...

avatar image Mol · Apr 03, 2015 at 09:53 AM 0
Share

i'm working on someone's else code these variables are static (and must stay static for some reasons) and i try to link them easily to slider without coding one specific function for each slider.

avatar image gjf · Apr 03, 2015 at 10:13 AM 0
Share

if each slider shares the same static variable, changing in one place will change all of them. the reference which you asked about is required, but the class variable cannot be static to use it in the way you need.

why must they remain static?

avatar image Mol · Apr 03, 2015 at 10:25 AM 0
Share

ok i think i didn't explain my issue correctly i don't want to edit the same static variable. i have various variable i want to edit their value using slider and i'm look for a way to write a method which given a slider and static variable will edit the variable value when the slider is modified. I can write a function for each of these value but i was trying to figure out a better way. $$anonymous$$aybe i'm missing something i'm new to unity and c#.

 //$$anonymous$$y $$anonymous$$olecule script
 
 public static float variable1 = 1.0f; // static variables cause they are visuals settings
 public static float variable2 = 1.0f; 
 public static float variable3 = 1.0f;
 
 

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

2 People are following this question.

avatar image avatar image

Related Questions

UI Slider; -x to +x range, 0 center; how can I get it to fill both directions starting from center? 3 Answers

UI slider "End slide" event 3 Answers

Use Slider for Geometry (GameObject) 'layers' 1 Answer

How to get an object to follow the Slider knob? 0 Answers

How to add sound to the movement of the radial slider 0 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