image fill amount

does someone know how to rest image fill amount?

code below does not do what i need . i need each time function clickedonDamageBucket() runs , after that fill amount get reset .

    void Start ()
    {
        anim = wateronfloor.GetComponent <Animator> ();
        TimeLeft = MaxTime;
    }
    void Update()
    {
        if (move)
        {
            if (TimeLeft > 0)
            {
                TimeLeft -= Time.deltaTime;
                Timerbar.fillAmount = TimeLeft / MaxTime;
            }
            else
            {
               
            }
        }
    }
    public void WaterOnFloor()
    {
        anim.Play ("animationWater");
    }

     void TimerBar()
    {
        move = true;
        TimerParent.SetActive (true);

        TimerParent.transform.position = new Vector3(wateronfloor.GetComponent <Animator> ().transform.position.x ,
            wateronfloor.GetComponent <Animator> ().transform.position.y + 0.5f,
            wateronfloor.GetComponent <Animator> ().transform.position.z);

    }   

    public void clickedonDamageBucket()
    {
        wateronfloor.GetComponent<Animator>().Play("Scene02Bucketwateranimation");
        this.GetComponent<Animator>().Play("Scene02Bucketanimation");

        TimerParent.SetActive (false);

    }

Just set it to 1?

1 Like

Looks like you need to reset TImeLeft = MaxTime, since you are updating your fillAmount in Update based on the TimeLeft variable.

1 Like
    IEnumerator PlayAnimsRandomly()
    {
        while(!done)
        {
            int delay = Random.Range(6,15);

            Animatedobjects[0].GetComponent<Animator>().Play("animation");
            Animatedobjects[1].GetComponent<Animator>().Play("animation");
            Animatedobjects[2].GetComponent<Animator>().Play("animation");
            Animatedobjects[3].GetComponent<Animator>().Play("animation");
            Animatedobjects[4].GetComponent<Animator>().Play("animation");
            Animatedobjects[5].GetComponent<Animator>().Play("animation");
            Animatedobjects[6].GetComponent<Animator>().Play("animation");
            Animatedobjects[7].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds((int)delay);
        }

    }

here delay time does not work. all animations happen at once each time. how can i make them completely random? thanks also for above replies. i consider those and tell you how they work.

I’m not sure what you are expecting. You’re telling it to play all animations, one after the other. If you need one to play, then the next to start, you either need to yield between them or yield in a for loop that plays each one in a row.

If you want it to pick a random animation, you need a random for your index and you play that one with the delay after it.

1 Like

i have already yield between them , but it was not nice. as you said exactly i need one animation runs. then another one after few seconds run. can you please give me a snippet? i am searching alot. thanks.

Your question isn’t completely clear. Just take a moment to write it down in point form, maybe.
If you had a yield in between them, they would play in order, after each yield.

If you want them to play in a random order, but you also play through each of them, that is something else.

The answer “I did that, but it was not nice” isn’t very descriptive. :slight_smile: What was not nice about it? heh.

1 Like
    IEnumerator PlayAnimsRandomly()
    {
        while(!done)
        {
            yield return new WaitForSeconds(Random.Range(3,6));
            Animatedobjects[0].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds(Random.Range(2,6));
            Animatedobjects[1].GetComponent<Animator>().Play("animation");

//            yield return new WaitForSeconds(Random.Range(3,6));
            Animatedobjects[2].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds(Random.Range(2,6));
            Animatedobjects[3].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds(Random.Range(3,6));
            Animatedobjects[4].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds(Random.Range(2,6));
            Animatedobjects[5].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds(Random.Range(3,6));
            Animatedobjects[6].GetComponent<Animator>().Play("animation");

            yield return new WaitForSeconds(Random.Range(3,6));
            Animatedobjects[7].GetComponent<Animator>().Play("animation");

        }

    }

i did in between but this also is not good.
i need each time randomly at least 3 different animation , run. i need this . thanks for your idea?

I was in the process of writing an answer, but your update post appeared and seems to contradict what you said before.

Please take some time to properly word what it is you’re trying to do :slight_smile: That will help not only readers, but yourself in being able to understand, and execute your design plan.

First I thought you wanted every animation to play, but in a random order. (one time through? I wasn’t sure).

Now you wrote that you want 3 different animations to play.

Anyhow, I will continue writing about what I was going to say, and you can cut it down to 3 if that’s your goal.

Write a list that is the same size as the number of animations (0 through 7 - 8 elements it looks like).
On each iteration of your loop, choose a random integer value from that list, and play the animation at that index. Also, remove the chosen integer from the list. Repeat this for 3 - 8 loops, as desired, inserting your proper (random) delay between each.

Hopefully that makes sense… :slight_smile:

1 Like

For a small 3 set, it might look like:
List of integers : 0, 1, and 2.

Loop:
get random from List
Play animation at index from said random.
remove the number from the list of integers.
wait for your delay.
(repeat until # of loops is satisfied, or until list of integers is 0).

1 Like

can you please give me a snippet? because what you explained is exactly what i mean by this question . thanks.good luck!

You don’t need to wish me good luck!

I’ll wish you good luck!

Take some time and try to follow what I wrote to write a script that you can use. You only wrote back immediately without even giving it some effort.
If you get stuck, you should come back and explain what happened, showing your code, etc… :wink:

1 Like
    IEnumerator PlayAnimsRandomly()
    {
        while(!done)
        {

            List<int> AnimationsList = new List<int>() { 1, 2, 3};

            for(int i = 1; i <= 3; i++)
            {
                AnimationsList.Animatedobjects.[0].GetComponent<Animator>().Play("animation");
            }

its not working.

What happen to your yield? You’ll need one in your for loop.

It shouldn’t even compile, you must have a yield somewhere in the coroutine.

I do find it weird that it runs fine if the yield doesn’t get used but it still must have one

That’s not the only issue with the code, either.

1 Like

but no one said how to change fill amount as soon as one function is called ? here is what up to now i have done :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class timerrun : MonoBehaviour
{
    public GameObject TimerCounter;

    public void Item01return()
    {
        TimerCounter.SetActive (true);

    }
}

i have a ui image . it has a child . this child acts as one timer. only in function above i need as soon as this function , this child fill amount get from 1 to 0 .
thanls for any idea how to do it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class timerrun : MonoBehaviour
{
    public GameObject TimerCounter;

    public Image cooldown;
    private bool coolingDown;
    public float waitTime = 5.0f;

    void Update()
    {
        if (coolingDown == true)
        {
            //Reduce fill amount over 5 seconds
            cooldown.fillAmount -= 1.0f / waitTime * Time.deltaTime;
        }
    }

    public void Item01return()
    {
        TimerCounter.SetActive (true);
        coolingDown = true;
    }
}

i used unity documentation and it worked. thanks .

https://docs.unity3d.com/ScriptReference/UI.Image-fillAmount.html