• 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 /
  • Help Room /
avatar image
0
Question by radenzilu · Sep 20, 2016 at 08:46 PM · scripting problemscriptingbasicsslider

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?

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

1 Reply

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

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;
 }
Comment
Add comment · Show 11 · 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 TBruce · Sep 21, 2016 at 04:34 AM 1
Share

@radenzilu Did this work for you?

avatar image radenzilu · Sep 21, 2016 at 12:54 PM 0
Share

@Mavina First of all, thank you for your concern. I tried and it didn't work. Maybe, 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 : MonoBehaviour
 {
 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 <NavMeshAgent> ().enabled = false;
         GetComponent <Rigidbody> ().isKinematic = true;
         isSinking = true;
         ScoreManager.score += scoreValue;
         Destroy (gameObject, 2f);
     }
 }

Thanks Mavina, really thanks!

avatar image radenzilu radenzilu · Sep 21, 2016 at 01:02 PM 0
Share

@Mavina I forgot to call my method in my Update... Now it works, but I have another problem... My 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!

avatar image radenzilu radenzilu · Sep 21, 2016 at 02:21 PM 0
Share

@Mavina 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?

avatar image TBruce radenzilu · Sep 21, 2016 at 11:18 PM 1
Share

@radenzilu This is what I originally understand (please correct me where I am wrong)

  1. You have a slider

  2. The slider.value is incremented by an amount (in this case 10) till it reaches slider.maxValue (in this case 100)

  3. 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

Show more comments
Show more comments
avatar image radenzilu · Sep 22, 2016 at 08:58 PM 0
Share

Mavina, 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!

avatar image TBruce radenzilu · Sep 22, 2016 at 10:12 PM 1
Share

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

  1. I created a new project/scene.

  2. I added your class (commented out the reference to Fill and ScoreManager).

  3. I added a slider and button (to test the slider functionality)

  4. 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.

avatar image radenzilu TBruce · Sep 27, 2016 at 02:14 PM 0
Share

Hi, Mavina. Firts of all, sorry if I took too long to respond... My 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!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Learner Scripter... trying to make a basic move function. 1 Answer

How to set a special prefab in a switch? 1 Answer

Rotate character 0 Answers

JavaScript Problem - BCE0043 - Unexpected Token Help 0 Answers

Help with a C# script .SetActive 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges