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
1
Question by unityplease · Jan 21, 2015 at 05:37 PM · c#uislider

Create a slider which refers to a value in another script

I've got no answers to my other question here:

http://answers.unity3d.com/questions/878456/update-slider-every-frame-with-value-from-another.html

So I thought I'd re-write the problem with a bit more detail.

I dynamically create a series of sliders each representing a stat value (a float) defined as a public variable attached to a game object.

How to I ensure that each slider I create points to each of these appropriate values when they are created?

Is it possible to pass a reference to a value in another script when the slider object is created so that its value will be updated every frame from this value?

ie:

The gameobject has stats,

health, energy, mana, etc

These are attached to the game object as public floats in a script called Stats.

When I create sliders from a prefab these sliders need to have values set by these parameters but maintain a reference to them so they can be updated when they change, it is not good enough just to set the values initially because this just creates a local copy so that when they change, the value is not updated.

Please help. Details of the scripts are in the previous post.

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

2 Replies

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

Answer by Superrodan · Jan 22, 2015 at 01:00 AM

I think what you may want to do is have code like this pseudo-code below on the object you want to update the information in the slider:

 private Slider health;
 private Slider mana;
 private Slider thirdThing;
 
 public float healthValue;
 public float manaValue;
 public float thirdThingValue;
 
 void Start()
 {
 health = GameObject.Find("Slider1Name").GetComponent<Slider>();
 mana = GameObject.Find("Slider2Name").GetComponent<Slider>();
 thirdThing = GameObject.Find("Slider3Name").GetComponent<Slider>();
 }
 
 Update()
 {
 health.value = healthValue;
 mana.value = manaValue;
 thirdThing.value = thirdThingValue;
 }
Comment
Add comment · Show 3 · 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 unityplease · Jan 22, 2015 at 01:39 AM 0
Share

Yeah, that is probably a good way to do it. I was thinking of having the update script attached to each slider rather than the stats script because that might save overhead when the sliders were disabled. But I could add in a enabled check before each update. From a technical point of view I wonder if it is still possible to pass a reference to a public parameter to another script? Because that would enable a generic update script to be attached to the slider with the (float reference) just being changed when it was created. But in the absence of that I'll do it the way you suggested.

avatar image unityplease · Jan 22, 2015 at 01:47 AM 0
Share

Also for this to work I would have to ensure that the slider creation script is run before this one because otherwise the Find method will not return any sliders. From what I remember I do that in the monomanager script execution order dialogue, correct?

avatar image Superrodan · Jan 22, 2015 at 01:49 AM 0
Share

Can you put the slider creator in awake and this code in start? That would get it working in the correct order automatically.

avatar image
1

Answer by Mmmpies · Jan 21, 2015 at 06:23 PM

Didn't have time to answer your last one but it was on my to do list!

The new UI uses the event system so let's take a very basic example:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class StatsScript : MonoBehaviour {
 
 
     private float health;
 
     public Slider mySlider;
 
     public void SetHealth()
     {
         health = mySlider.value;
         Debug.Log("health = " + health);
     }
 }
 

Create a canvas add a Panel to it then put a slider on it. Drag that script onto the slider.

Next, with the slider selected click on the + in OnValueChanged at the bottom of the inspector.

A slot appears so drag the Slider onto the slot, that should open the script which is attached to slider so from the dropdown select StatsScript -> SetHealth.

The figures should update automatically.

The script can be on any gameObject, if you have several sliders then you'll need several to reference each one. You can use GameObject.Find if you don't want public variables but for speed and showing how the UI works public Sliders are easier.

Should end up looking like this:

Slider


slider.png (22.0 kB)
Comment
Add comment · Show 8 · 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 unityplease · Jan 22, 2015 at 12:52 AM 0
Share

Thanks for the detailed replay, but I actually want the reverse of this. Rather than changing the slider value and updating a stat. I want the slider value to update when I change the float. And I need to do it using c# scripting rather than drag and drop because my sliders are created dynamically by script.

avatar image Mmmpies · Jan 22, 2015 at 07:12 AM 0
Share

No problem but it is pretty easy to go from

health = mySlider.value;

To

mySlider.value = health;

That's the only change you needed.

avatar image isaacj11 · Jan 22, 2015 at 07:31 AM 0
Share

@$$anonymous$$mmpies Can you please help me with this,

I've set up a test health bar which decreases we have to touch it to increase it but ins$$anonymous$$d of touching the health bar gui I want to add a new Gui button and press it and I want it to respond to my code which increases health,

like a OnClickEvent idk, im a complete beginner to code..

avatar image Mmmpies · Jan 22, 2015 at 07:45 AM 0
Share

Assu$$anonymous$$g you're using the new 4.6 UI then yes but it's not really appropriate to ask a question in another question. Ask one yourself and show how you've set this up so far, I'm just going out but will look out for it in about half an hour.

avatar image Mmmpies · Jan 22, 2015 at 08:35 AM 0
Share

Odd I can't see it in the moderation queue, well I'm not much up on the old OnGUI myself as I'm fairly new to Unity myself (about a year or so), I was using DFGUI but moved to the new UI when that came out. Lookup On$$anonymous$$ouseDown though as I believe that can be be called for OnGUI items.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Get UI Slider Value 3 Answers

Trying to move slider based off percentage of screen 1 Answer

UI 4.6 add pointer enter event to button, c# (Not using Editor) 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