0
\$\begingroup\$

I'm implementing a fading effect for my game. I can change the duration to 0.1 (or 1, 5, or 10). But if I change the duration to 0 and the fading stops, then when I change back the duration to any value above 0 the fading does not resume. I tried it on the automatic mode when isAutomatic is true. I can't figure out why after setting it to 0 and back to 1, the fading is not continuing.

Here's my current implementation:

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

public class Fader : MonoBehaviour
{
    public GameObject objectToScale;
    public float duration = 1f;
    public Vector3 minSize;
    public Vector3 maxSize;
    public bool scaleUp = false;
    public Coroutine scaleCoroutine;
    public bool isAutomatic = false;

    private bool automatic = true;

    private void Start()
    {
        objectToScale.transform.localScale = minSize;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            Fade();
        }

        if (isAutomatic && automatic)
        {
            Fade();

            automatic = false;
        }
    }

    private IEnumerator ScaleOverTime(GameObject targetObj,
        Vector3 toScale, float duration)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            yield return null;
        }

        automatic = true;
    }

    private void Fade()
    {
        scaleUp = !scaleUp;

        if (scaleCoroutine != null)
            StopCoroutine(scaleCoroutine);

        if (scaleUp)
        {
            scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, maxSize, duration));
        }

        else
        {
            scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));
        }
    }
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Hint: try walking through the lines of code in the ScaleOverTime coroutine one by one, pretending you're the CPU deciding what to do when duration = 0. Where do you end up when you do this? \$\endgroup\$
    – DMGregory
    Commented Aug 25, 2022 at 23:56
  • 1
    \$\begingroup\$ @DMGregory if duration is 0 then counter will never be less then duration and therefore it will never enter and never reach to the yield return null; so the coroutine stop and never continue. \$\endgroup\$
    – Daniel Lip
    Commented Aug 26, 2022 at 0:04
  • 2
    \$\begingroup\$ Don't ask me: I am not the arbiter of whether your code is correct. Run your code and observe the outcome. You can do this far faster than waiting for a stranger on the Internet to get back to you. \$\endgroup\$
    – DMGregory
    Commented Aug 26, 2022 at 0:19
  • 1
    \$\begingroup\$ Do you get an exception in your Unity console? Perhaps something hinting that you get a division by zero? When you get an unhandled exception in a coroutine, then that coroutine will crash. \$\endgroup\$
    – Philipp
    Commented Aug 26, 2022 at 11:23

1 Answer 1

0
\$\begingroup\$

For first situation, setting your duration = 0 will not enter this loop:

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            yield return null;
        }

Hence, object not fading.

Second situation, as for you changing your duration from 0 to 1 in runtime, the line scaleUp = !scaleUp; is setting scaleUp = true at 0, even though the object didn't scale up, now when you increase the duration from 0 to t seconds ( assuming t > 0) , it will again call the scaleUp = !scaleUp; that in return turns scaleUp = false, hence the code executing this line

scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, minSize, duration));

i.e. going from objectToScale.transform.localScale to minSize which is the same as objectToScale.transform.localScale as set in the Start() function.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .