1

I created a function that will run until an images fillamount is at 0. However when I call this function from another class the unity editor completing freezes. I cant even stop pause play mode.

The class that calls the cooldown function.

Cooldown cooldown;
    
cooldown = gameObject.GetComponentInChildren<Cooldown>();//Sets cooldown to have the same values as the prefab.
    
    public void gatherCooldown()
    {
        cooldown.resourceCooldown();
    }

Cooldown Class

public class Cooldown : MonoBehaviour
{
    public Image imageCooldown;
    public float cooldown = 5;
    public bool isCooldown

    public void resourceCooldown()
    {
        while (imageCooldown.fillAmount >= 0)
        {
            imageCooldown.fillAmount -= 1 / cooldown * Time.deltaTime;
        }

    }
}
4
  • I'm far from a Unity expert, but shouldn't you have some delay in your while loop? Because if deltatime is close enough to 0, wouldn't that means you have a near infinite loop ? Commented Nov 30, 2020 at 8:39
  • 2
    What exactly are you trying to do? Running a blocking method like that is nearly never a good idea, as it has to stop the execution of everything else. I suspect your method should be a Coroutine instead Commented Nov 30, 2020 at 8:40
  • i just want to have the player remain in the same position until the images fill amount is 0 then destroy that object. I tried to use a coroutine but I'm new and kinda got confused by them Commented Nov 30, 2020 at 8:41
  • did not even read the code, but freeze = infinite loop. You need to be careful using whiles and ensure that the code moves on Commented Nov 30, 2020 at 8:47

2 Answers 2

2

You are currently using a blocking method. To fix that you could use a Corountine instead to decreasing your images fillAmount.

Coroutine Example:

private IEnumerator DecreaseFillAmount() {
    // Entered the Coroutine
    isCooldown = true;
    // Repeat until the fillAmount is smaller than or equal to 0
    while (imageCooldown.fillAmount > 0) {
        imageCooldown.fillAmount -= (1 / cooldown) * Time.deltaTime;
        yield return null;
    }
    // Left the Coroutine
    isCooldown = false;
}

You also need to make sure that you call your Coroutin the right way, you achieve this with StartCoroutine().

Calling the Coroutine:

public void resourceCooldown() {
    // Call the DecreaseFillAmount Coroutine.
    StartCoroutine(DecreaseFillAmount());
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your while is freezing the main thread.

This should probably rather be a Coroutine

public class Cooldown : MonoBehaviour
{
    public Image imageCooldown;
    public float cooldown = 5;
    public bool isCooldown;

    private IEnumerator CooldownRoutine()
    {
        isCooldown = true;

        while (imageCooldown.fillAmount >= 0)
        {
            imageCooldown.fillAmount -= 1 / cooldown * Time.deltaTime;

            // Tells Unity to "pause" this routine, render this frame
            // And continue from here in the next frame
            yield return null;
        }

        isCooldown = false;
    }

    public void resourceCooldown()
    {
        StartCoroutin(CooldownRoutine ());
    }
}

1 Comment

Thanks the courtine worked. I tried something similar to this earlier and didn't see to work so thanks so much for both of your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.