为什么我的Unity代码中没有延迟?

huangapple go评论62阅读模式
英文:

Why is there not a delay within my Unity code?

问题

我目前正在处理两个脚本:一个是在接触后两秒内摧毁目标,另一个是激活下一个目标。尽管我对两者都使用了协程和调用,但我仍然没有得到延迟。有什么想法吗?"FootballContact" 脚本连接到足球,而 "TargetActivate1" 脚本附加到第一个目标。

Football Contact 脚本

TargetActivate1 脚本

我尝试使用协程延迟目标的销毁,但我的新 "TargetActivate1" 脚本使目标的激活与目标销毁同时发生,使销毁/激活过程瞬间完成。因此,我尝试在新脚本上使用相同的协程函数 - 它不起作用。互联网上说我不能同时运行两个协程,所以我将协程更改为 "invoke"。它仍然不起作用,我非常困惑下一步该做什么。感谢任何帮助! 为什么我的Unity代码中没有延迟?

英文:

I am currently working on two scripts: one that destroys a target after two seconds after contact has been made, and another that activates the next target. Although I have used coroutines and invokes for both, I still am not getting a delay. Any thoughts? The "FootballContact" script is connected to the football and the "TargetActivate1" script is attached to the first target

Football Contact Script

TargetActivate1 Script

I tried to use a coroutine to delay the target from being destroyed, but my new "TargetActivate1" script makes it so the activation of the targets overlaps with when the target is destroyed--making the destroying/activation process instant. As a result, I tried using that same coroutine function on the new script--it didn't work. The internet said I can't have two coroutines running at the same time, so I changed the coroutine to an "invoke". It still does not work and I'm very confused as to what to do next. I appreciate any help! 为什么我的Unity代码中没有延迟?

答案1

得分: 1

使用IEnumerator来为两个脚本都简化操作。

原因是它不起作用的原因是您需要将希望在延迟之后运行的代码放在IEnumerator函数内部。

协同程序的工作方式是,当您调用StartCoroutine时,IEnumerator函数将被调用,并将独立于您调用它的方法而运行。因此,在StartCoroutine下面的任何代码都将像平常一样继续运行。

对于您的FootballContact脚本,例如,代码应该如下所示:

private IEnumerator Delay(){
  yield return new WaitForSeconds(2f);
  DestroyObject(target);
}
英文:

Use the IEnumerator for both scripts to make it easier😁

The reason it isn't working is because you need to place the code, that you would like to run after the delay, within the IEnumerator function.

How coroutines work is that when you call StartCoroutine the IEnumerator function will be called and will be run independently of the method in which you called it. So, any code underneath the StartCoroutine will continue as normal.

For you FootballContact script, for example, the code should look like this:

private IEnumerator Delay(){
  yield return new WaitForSeconds(2f);
  DestroyObject(target);
}

答案2

得分: 1

销毁代码本身有延迟,但如果您想要更专业的方法,请了解 `Async`。
销毁(gameObject, 2f);

异步示例:

private async void OnTriggerEnter(Collider other)
{
    Debug.Log("等待前!");
    await Task.Delay(2000); // 以毫秒为单位。
    Debug.Log("等待后!");
}
英文:

The destroy code itself has a delay, but if you want a more professional method, read about Async.

Destroy(gameObject, 2f);

Asynchronous example:

private async void OnTriggerEnter(Collider other)
{
    Debug.Log("Before Wait!");
    await Task.Delay(2000); // in milliseconds.
    Debug.Log("After Wait!");
}

huangapple
  • 本文由 发表于 2023年2月27日 02:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定