Question by
Convieo · Aug 23, 2021 at 03:16 AM ·
spriterenderercoroutines
Trying to open/close shutter using SpriteRenderer.size and coroutines
So far, the shutter opens just fine. I'm using SpriteRenderer.size to make the shutter basically invisible because that way the texture looks like a shutter going up.
However, when the second coroutine is started, the shutter increases in size forever. I'm new to coding and Unity, and I figured copying the code and just changing the values would do the job, but obviously I'm missing something. The code is initiated after a van has finished (shutter opens)/started (shutter closes) moving using iTween.
public class ShutterControl : MonoBehaviour
{
void InitiateOpenShutter()
{
StartCoroutine("OpenShutter");
}
IEnumerator OpenShutter()
{
InvokeRepeating("OpenShutterAnimation", 0, 0.1f);
yield return new WaitForSeconds(1.9f);
GetComponent<BoxCollider2D>().enabled = false;
CancelInvoke("OpenShutterAnimation");
StopCoroutine("OpenShutter");
}
void OpenShutterAnimation()
{
GetComponent<SpriteRenderer>().size -= new Vector2(0f, 0.05f);
}
void InitiateCloseShutter()
{
StartCoroutine("CloseShutter");
}
IEnumerator CloseShutter()
{
InvokeRepeating("CloseShutterAnimation", 0, 0.1f);
yield return new WaitForSeconds(1.9f);
GetComponent<BoxCollider2D>().enabled = true;
CancelInvoke("CloseShutterAnimation");
StopCoroutine("CloseShutter");
}
void CloseShutterAnimation()
{
GetComponent<SpriteRenderer>().size += new Vector2(0f, 0.05f);
}
}
I thought maybe the two coroutines were interacting with each other, so I made them stop after executing, but that didn't help. This is probably the wrong way to go about doing this, so if there's any better ways then please let me know! Thanks.
Comment