- Home /
Updating UI Slider through another script
Right now I have a script controlling a slider basically making it at like a game meter. The meter starts at a max value in this case 10 and over time the meter decreases in value. (So a 10 second timer/meter)
static var val : float = 10f;
var meter : GameObject;
function Update() {
if(gameStart == true) {
if(objectController.objectDestroyed == true) {
meter.value = meter.value + val;
} else {
meter.value -= Time.deltaTime;
}
}
}
I'm checking my object controller to see if the object has been destroyed then, if it has, add more time to the meter basically keeping you alive unless it hits 0.
function OnMouseDown() {
Destroy(gameObject);
// Set SD to true
objectDestroyed = true;
// Add time to meter
levelUI.val = 10f;
// Reset SD to false
objectDestroyed = false;
}
So since I couldn't change the actual slider value via my objectController script. I decided to add a boolean to check whether or not the object has been destroyed. The code isn't breaking, but when I check the console to what objectDestroyed is, it's always false. Am I doing this too fast, or is this just not the way it should be done.
Answer by adrianrodriguez · Jan 16, 2015 at 03:13 PM
Going to go ahead and answer my question here, I needed to move things around so they weren't both happening at the same time.
function Update() {
if(objectDestroyed) {
// Add time to meter
levelUI.val = .5f;
// Reset SD to false
objectDestroyed = false;
}
}
function OnMouseDown() {
Destroy(gameObject);
objectDestroyed = true;
}
Hope this helps someone in the future.