How to Use IEnumerator Correctly when button calls multiple functions

非 Y 不嫁゛ 提交于 2020-06-29 04:33:41

问题


I am making a card game in which I am trying to make a (0.5f) delay before each card is instantiated. I have my code which instantiates and object

public IEnumerator Name(int x,int y, int z)
{

}    

In the IEnum i have a yeild return new WaitForSeconds(0.5f)before all the code with the instantiation.

I call my IEnumerator in 2 different classes 2 times in each by using

StartCoroutine(Name(...par...));

And on my play game button i have 4 events which use the enum to spawn the cards but there is no delay .

Is there a way to make the cards appear 1 by one .

Thanks for any support.


回答1:


Whatever it is that is currently calling StartCoroutine needs to be the coroutine.

Right now you have code that looks/behaves like this:

StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));

And all of them are spawning a card and not waiting for each other. You don't want this, so you're going to need to make a fundamental change in how your calls are made so that you can get this behavior:

StartCoroutine(SomeMethod(...));

IEnumerator SomeMethod(...) {
    yield return Name(...par...)
    yield return Name(...par...)
    yield return Name(...par...)
    yield return Name(...par...)
}


来源:https://stackoverflow.com/questions/52194492/how-to-use-ienumerator-correctly-when-button-calls-multiple-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!