I’m trying to call a function from my PlayerController class from another class, but for some reason the function is never getting called. Any ideas why?
private void FreezePlayers(GameObject player)
{
string playerID = player.GetComponent<PlayerModel>().playerID;
foreach(GameObject p in GameManager.instance.players)
{
if(p.GetComponent<PlayerModel>().playerID != playerID)
{
Debug.Log("Freeze Function Reached");
p.GetComponent<PlayerController>().Freeze();
}
}
}
In the PlayerController class:
public IEnumerable Freeze()
{
Debug.Log("Freeze Function Called");
isFrozen = true;
Transform iceBlock = Instantiate(GameManager.instance.iceBlockPrefab, transform.position, Quaternion.identity);
iceBlock.parent = transform;
yield return new WaitForSeconds(2.0F);
isFrozen = false;
Destroy(iceBlock);
}
I know the freeze function is getting reached because of the print statements I added.
“Freeze Function Reached” gets printed but “Freeze Function Called” never gets printed.
Freeze() is an IEnumerable.
You need to call it as a Coroutine
StartCoroutine(p.GetComponent<PlayerController>().Freeze());