- Home /
Question by
JoeyWeidman · Nov 10, 2017 at 01:26 AM ·
function call
Function from other script not getting called.
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.
Comment
Best Answer
Answer by JacobFlynnPrendi · Nov 10, 2017 at 01:57 AM
Freeze() is an IEnumerable.
You need to call it as a Coroutine
StartCoroutine(p.GetComponent<PlayerController>().Freeze());
Also i just noticed that you need to change "IEnumerable" to "IEnumerator"
I've made that IEnumerable error so many times my god