I want to start function StartGame() after my Animation ends. I have this function:
using UnityEngine;
public class DominoAnimation : MonoBehaviour
{
private Vector2 startPos = new Vector2(11f, 0f);
public Vector2 endPos;
private float step = 0.03f;
private float progress;
private bool isAnimationEnd = false;
public delegate void Event();
public event Event onAnimationEnd;
private void FixedUpdate()
{
if (progress >= 1f && !isAnimationEnd)
{
GetComponent<SpriteRenderer>().color = new Color(0.5f, 0.5f, 0.5f);
isAnimationEnd = true;
onAnimationEnd();
}
transform.position = Vector2.Lerp(startPos, endPos, progress);
progress = step;
}
}
And when function in fixedUpdate finishes, I want to start another func in another class:
public class OnlineGame : MonoBehaviour
{
private void StartGame()
{
// Starting game
if (DataController.MaxPlayerDomino[0] > DataController.MaxEnemyDomino[0])
{
DataController.move = true;
domino[DataController.MaxPlayerDomino[1]].GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f);
}
else DataController.move = false;
// Else not one have double
}
}
I try to do this through an event, but I got an error through the fact that the reference to the DominoAnimation class is not working
CodePudding user response:
You can use IEnumerator to to wait until isAnimationEnd is true like this :
private IEnumerator StartGame()
{
yield return new WaitUntil(() => DominoAnimation.instance.isAnimationEnd);
// Starting game
if (DataController.MaxPlayerDomino[0] > DataController.MaxEnemyDomino[0])
{
DataController.move = true;
domino[DataController.MaxPlayerDomino[1]].GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f);
}
else DataController.move = false;
// Else not one have double
}
and call this function as a StartCoroutine(OnlineGame.instance.StartGame());
do not forgot to make instance of class and methods publics to you can call them on another class.
