问题描述
我有一个3秒的倒数计时器,当游戏未暂停时会启动.我几天前就让它正常工作,但现在不再工作了.它在数字3上被阻止.这是代码:
I have a 3 second countdown timer which is activated when game is unpaused.I had it working correctly a couple of days ago but now it doesn't work anymore. It gets blocked on the number 3. This is the code:
IEnumerator Timer() {
Time.timeScale = 0;
objectWithGSScript.scoreText.fontSize = 300;
objectWithGSScript.scoreText.text = "" + 3;
yield return WaitOneSecond();
objectWithGSScript.scoreText.text = "" + 2;
yield return WaitOneSecond();
objectWithGSScript.scoreText.text = "" + 1;
yield return WaitOneSecond();
objectWithGSScript.scoreText.text = "Go!";
yield return WaitOneSecond();
Time.timeScale = 1f;
objectWithGSScript.scoreText.text = objectWithGSScript.score.ToString();
}
IEnumerator WaitOneSecond() {
float start = Time.realtimeSinceStartup;
while (Time.realtimeSinceStartup < start + 1f) {
print("entered");
yield return null;
}
}
它只打印一次输入",似乎它从协程退出,就像它永远返回null一样.
It prints "entered" only once, it seems it exit from the coroutine, like it's returning null forever.
可能是什么问题?
任何帮助将不胜感激.
推荐答案
您代码中的函数非常好.不,它不会也不应该停止在数字3处.
The function in your code is perfectly fine. No, it does not and should not stop at the number 3.
这些可能是它表现为停止在数字3处的可能原因.
These are the possible reasons why it is behaving like it is getting stopped at the number 3.
1 .您正在呼叫StopCoroutine
或或StopAllCoroutines
.请检查您是否没有停止协程.如果您正在运行,那么它将以这种方式运行.
1.You are calling StopCoroutine
or or StopAllCoroutines
. Please check that you are not stopping the coroutines. If you are when it is running, then it will behave this way.
2 .您正在销毁该脚本所附加的脚本或GameObject.检查在何处调用Destroy(gameObject);
,Destroy(this);
或类似名称.如果脚本被销毁,协程应停止运行.
2.You are destroying the script or GameObject this script is attached to. Check where you call the Destroy(gameObject);
, Destroy(this);
or something similar. If the script is destroyed, the coroutine should stop running.
请记住,您可以从另一个脚本中销毁一个脚本,所以请检查所有脚本.
Remember that you can destroy a script from another script so check all scripts.
3 .您已禁用脚本附加到的GameObject.当您禁用GameObject时,协程将停止工作.检查您是否没有gameObject.SetActive(false);
或带有SetActive(false);
的任何可禁用该GameObject的东西.
3.You disabled the GameObject that his script is attached to. When you disable a GameObject, the coroutine stops working. Check that you don't have gameObject.SetActive(false);
or anything with SetActive(false);
that disables that GameObject.
4 .如果您在 ScriptA 中具有协程功能,然后在销毁 ScriptB ,来自 ScriptA 的协程将冻结在yield return语句处.了解这一点很重要.
4.If you have a coroutine function in ScriptA and then starts that coroutine from ScriptB, if you destroy ScriptB, the coroutine from ScriptA will freeze at the yield return statement. It is important that you know this.
5 .空问题...
也许objectWithGSScript.scoreText.text
不是null
.您必须检查每个变量,并确保它们不是null
. if
弹力很好,但这是一个很好的捷径:
Maybe the objectWithGSScript.scoreText.text
is not null
. You must check each variable and make sure that they are not null
. The if
stametement is fine but a good shortcut is this:
UnityEngine.Debug.Log(objectWithGSScript);
UnityEngine.Debug.Log(objectWithGSScript.scoreText);
UnityEngine.Debug.Log(objectWithGSScript.scoreText.text);
然后您可以做:
objectWithGSScript.scoreText.fontSize = 300;
objectWithGSScript.scoreText.text = "" + 3;
我想不出发生这种情况的任何其他可能原因,但请检查上面提到的所有五件事.
这篇关于为什么协程停止工作/执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!