How to decrease a Slider value by time?
Hi! I have a slider that updates 10 by 10 from 0 to 100 when an Enemy is dead. When it arrives to 100, I want to decrease my slider in 10 seconds. I know, maybe it's a newbie question, but I didn't find an answer in anywhere.
Here is my EnemyHealth code that increases the slider:
public Slider shootSlider; public int powerCharge = 10; void Death () { isDead = true;
capsuleCollider.isTrigger = true;
anim.SetTrigger ("Dead");
enemyAudio.clip = deathClip;
enemyAudio.Play ();
//Here my slider is increased when an enemy dies
shootSlider.value += powerCharge;
}
Can somebody help me, please?
Answer by TBruce · Sep 21, 2016 at 02:14 AM
Tested and works
public Slider enemySlider;
public void DecreseEnemySlider()
{
StartCoroutine(DecreseSlider(enemySlider));
}
IEnumerator DecreseSlider(Slider slider)
{
if (slider != null)
{
float timeSlice = (slider.value / 10);
while (slider.value >= 0)
{
slider.value -= timeSlice;
yield return new WaitForSeconds(1);
if (slider.value <= 0)
break;
}
}
yield return null;
}
@$$anonymous$$avina First of all, thank you for your concern. I tried and it didn't work. $$anonymous$$aybe, it's because I am a newbie. LOL Anyway, your code open my eyes for a lot of things here. I'll paste my code here so you can see what I did wrong.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EnemyHealth : $$anonymous$$onoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Slider shootSlider;
public Image Fill;
public int powerCharge = 10;
public int powerReset = 20;
public float sinkSpeed = 2.5f;
public int scoreValue = 10;
public AudioClip deathClip;
Animator anim;
AudioSource enemyAudio;
ParticleSystem hitParticles;
CapsuleCollider capsuleCollider;
bool isDead;
bool isSinking;
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
if(isSinking)
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
UpdateHealthBar ();
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
hitParticles.transform.position = hitPoint;
hitParticles.Play();
if (currentHealth <= 0) {
Death ();
}
}
void Death ()
{
isDead = true;
capsuleCollider.isTrigger = true;
anim.SetTrigger ("Dead");
enemyAudio.clip = deathClip;
enemyAudio.Play ();
shootSlider.value += powerCharge;
}
public void UpdateHealthBar() {
if (shootSlider.value >= 100) {
Fill.color = Color.cyan;
}
}
public void DecreseEnemySlider()
{
StartCoroutine(DecreseSlider(shootSlider));
}
IEnumerator DecreseSlider(Slider slider)
{
if (slider != null)
{
float timeSlice = (slider.value / 10);
while (slider.value >= 0)
{
slider.value -= timeSlice;
yield return new WaitForSeconds(1);
if (slider.value <= 0)
break;
}
}
yield return null;
}
public void StartSinking ()
{
GetComponent <Nav$$anonymous$$eshAgent> ().enabled = false;
GetComponent <Rigidbody> ().is$$anonymous$$inematic = true;
isSinking = true;
Score$$anonymous$$anager.score += scoreValue;
Destroy (gameObject, 2f);
}
}
Thanks $$anonymous$$avina, really thanks!
@$$anonymous$$avina I forgot to call my method in my Update... Now it works, but I have another problem... $$anonymous$$y Slider stopped to charge until 100. Now it's charge until 10 points and start to decrease. Any ideia what is happen? Thank you so much, my friend! I'll vote you here!
@$$anonymous$$avina I understood why this isn't charging... That's because you made this code to works with a value starting with 100 in slider and I want to start with 0, kill enemies until reach 100. Setting the slider value to 100, it works perfect!!! But I want to active this "decreasing function" when my slider reach 100. I am adding 10 points in every Enemy death, so when I kill 10 enemies, I reach 100 and when I reach 100, this starts to decreasing by time with the code you help me. Do you have any ideia how can I do that?
@radenzilu This is what I originally understand (please correct me where I am wrong)
You have a slider
The slider.value is incremented by an amount (in this case 10) till it reaches slider.maxValue (in this case 100)
When slider.value reaches slider.maxValue (100) you want the value to decrease back down to 0 over 10 seconds at 10 points per second
I am still not 100% sure how this differs from what I provided but here are some slight modifications to your script that may remedy some of your problems
$$anonymous$$avina, your code and my code are both working fine and decreasing the slider. The problem is that it doesn't go from 100 to 0... It stops before reach 0 when is decreasing. The weird thing is when I press play, it works diffferent every time. Sometimes it decreases until the half of slide, sometimes just a little slice and sometimes go near to 0, but doesn't reach. Do you know what is happening? Thanks anyway!
First my apologies, there was a small error in my code. This
if ((!shootSliderDecreasing) && (slider.value >= slider.maxValue))
inside DecreseEnemySlider()
, should have been this
if ((!shootSliderDecreasing) && (shootSlider.value >= shootSlider.maxValue))
Now I had already tested the DecreseSlider()
coroutine within one of my other projects and everything had worked fine. So I then went and did the following
I created a new project/scene.
I added your class (commented out the reference to Fill and Score$$anonymous$$anager).
I added a slider and button (to test the slider functionality)
Had the button call DecreseEnemySlider()
Everything again worked fine. So I reread your question and saw the main difference is that I was using 1
as the shootSlider.maxValue
for the slider where you were using 100
(where there should really be no problem). So I changed shootSlider.value
and shootSlider.maxValue
to 100. Testing resulted in the same problem you are currently having. So I went and rewrote the DecreseSlider()
coroutine (along with some other small changes).
And here is the new code. This is tested and works with any value for shootSlider.value
.
Hi, $$anonymous$$avina. Firts of all, sorry if I took too long to respond... $$anonymous$$y friend, I am still getting the same problem and I am starting to thinks thats an Unity bug, because each time I play my game, it has a different behavior. Sometimes the slider decreases from 100 to 60 (approximately), sometimes decreases just a little slice, sometimes doesn't decreases. I don't know what's happening.
Anyway... Thanks my friend!
Your answer
Follow this Question
Related Questions
How do i fix this problem 1 Answer
Help with a C# script .SetActive 1 Answer
How to add animations to character ? 0 Answers
AddForce Isn't working? 1 Answer