- Home /
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.
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;
}
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.
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?
Can you put the slider creator in awake and this code in start? That would get it working in the correct order automatically.
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:
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.
No problem but it is pretty easy to go from
health = mySlider.value;
To
mySlider.value = health;
That's the only change you needed.
@$$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..
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.
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.
Your answer
Follow this Question
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