Search code examples
c#unity-game-enginecoroutines

I have a co routine that once selected plays through. However If i go to select it again nothing happens. It does work the first time however


I have a co routine that once selected plays through. The co routine scales an object up. The second one it's selected it scales down the object.

However If i go to select it again nothing happens. It does work the first time however.

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

public class square : MonoBehaviour
{

    public Transform Button;
    float ElapsedTime = 0.0f;
    float TotalTime = 0.4f;

    private bool _isenlargingcanvas;

    public void enlargecanvas()
    {
        if (_isenlargingcanvas)
            return;
        _isenlargingcanvas = true;
        StartCoroutine(transitionscale());
        _isenlargingcanvas = false;
    }

    IEnumerator transitionscale()

    {

        while (ElapsedTime < TotalTime)
        {
            ElapsedTime += Time.deltaTime;
            Button.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new
            Vector3(9, 7, 7), (ElapsedTime / TotalTime));
            yield return null;
        }
    }

    private bool _isshrinkingcanvas;

    public void shrinkcanvas()
    {
        if (_isshrinkingcanvas)
            return;
        _isshrinkingcanvas = true;
        StartCoroutine(transitionscaledown());
        _isshrinkingcanvas = false;
    }

    IEnumerator transitionscaledown()

    {

        while (ElapsedTime < TotalTime)
        {
            ElapsedTime += Time.deltaTime;
            Button.localScale = Vector3.Lerp(new Vector3(9, 7, 7), new
            Vector3(0, 0, 0), (ElapsedTime / TotalTime));
            yield return null;
        }
    }

}

I have a co routine that once selected plays through. The co routine scales an object up. The second one it's selected it scales down the object.

However If i go to select it again nothing happens. It does work the first time however.


Solution

  • It looks to me like you're not resetting the ElapsedTime field so if you were to re-enter the transitionscale() method the condition in the while statement is already false so the method exits without doing anything.

    Possible solutions to this are, the reset the variable before calling the method each time... as follows

    // Inside the elarge canvas method
    ElapsedTime = 0.0f;
    StartCoroutine(transitionscale());
    

    Alternatviely, and with less work, you can reset it inside the tranisition scale method.

    IEnumerator transitionscale()
    
    {
        ElapsedTime = 0.0f;
    
        while (ElapsedTime < TotalTime)
        {
            ElapsedTime += Time.deltaTime;
            Button.localScale = Vector3.Lerp(new Vector3(0, 0, 0), new
            Vector3(9, 7, 7), (ElapsedTime / TotalTime));
            yield return null;
        }
    }
    

    You'll need to do the same for the trasitionscaledown() method. Consider one of those changes and see if that solves your problem.