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.
It looks to me like you're not resetting the
ElapsedTimefield so if you were to re-enter thetransitionscale()method the condition in thewhilestatement 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
Alternatviely, and with less work, you can reset it inside the tranisition scale method.
You'll need to do the same for the
trasitionscaledown()method. Consider one of those changes and see if that solves your problem.