You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity协程中WaitForSeconds未正常工作问题排查求助

问题诊断与修复方案

核心问题

你当前的协程启动逻辑存在致命错误:在Bullet中通过StartCoroutine(target.GetComponent<Enemy>().Poisoned(...))启动协程时,协程会被绑定在Bullet对象上运行。如果子弹击中目标后被销毁(这是多数子弹逻辑的常规操作),绑定在它身上的协程会被Unity直接终止,自然执行不到WaitForSeconds之后的代码。

修复步骤

1. 重构Enemy脚本:让自身管理毒伤协程

将协程改为内部逻辑,对外暴露启动方法,确保协程由Enemy对象持有,不受子弹生命周期影响:

public class Enemy : MonoBehaviour
{
    private Coroutine _poisonCoroutine;
    public float currentHp; // 替换为你实际的当前血量变量

    // 对外的毒伤启动接口
    public void ActivatePoison(float damagePerSecond)
    {
        // 避免重复叠加毒伤
        if (_poisonCoroutine != null)
        {
            StopCoroutine(_poisonCoroutine);
        }
        _poisonCoroutine = StartCoroutine(PoisonLoop(damagePerSecond));
    }

    // 内部毒伤循环协程
    private IEnumerator PoisonLoop(float damagePerSecond)
    {
        while (currentHp > 0)
        {
            TakeDamage(damagePerSecond);
            Debug.Log("Poisoned: 造成持续伤害");
            yield return new WaitForSeconds(1);
        }
        // 血量归0后销毁自身(若TakeDamage未处理销毁)
        Destroy(gameObject);
    }

    public void TakeDamage(float damage)
    {
        currentHp -= damage;
        if (currentHp <= 0)
        {
            // 提前终止协程,避免多余循环
            if (_poisonCoroutine != null)
            {
                StopCoroutine(_poisonCoroutine);
            }
            Destroy(gameObject);
        }
    }
}

2. 修改Bullet脚本:调用Enemy的毒伤启动方法

不再直接启动协程,转而让Enemy自身控制协程生命周期:

public class Bullet : MonoBehaviour
{
    public bool Poisonous = false;
    public float PoisonDamagePerSecond;

    void DamageTarget()
    {
        Enemy enemy = target.GetComponent<Enemy>();
        if (enemy != null)
        {
            // 执行你的非毒伤害逻辑...
            if (Poisonous)
            {
                enemy.ActivatePoison(PoisonDamagePerSecond);
            }
            // 子弹命中后正常销毁
            Destroy(gameObject);
        }
    }
}

额外优化说明

  • 新增重复毒伤判断,防止同一敌人被多次触发毒伤导致伤害叠加异常。
  • 用while循环替代递归调用协程,逻辑更直观,同时避免递归可能引发的栈溢出风险。
  • 在TakeDamage中提前终止协程,确保敌人血量归0后立刻停止毒伤逻辑,避免无效执行。

内容的提问来源于stack exchange,提问作者Lexus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 14:02:07