【问题标题】:How can I make the StartCoroutine working in unity?如何使 StartCoroutine 统一工作?
【发布时间】:2020-06-05 11:21:39
【问题描述】:

我是 C# 的新手,我正在尝试制作一个平台,当玩家站在它上面时它会掉下来。我使用了 StartCoroutine,所以平台在五秒钟后掉了下来,但由于某种原因,我的 couroutine 不起作用。

这是代码:

public GameObject player;




   private Rigidbody rb;

    void Start()
    {
       rb = GetComponent<Rigidbody>();

       rb.useGravity = false;

        //rb.isKinematic = false;
    }

    // Update is called once per frame
    void Update()
    {

    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "player")

        {

            StartCoroutine(ObjectFall());

        }
    }

    IEnumerator ObjectFall()
    {

        yield return new WaitForSeconds(5f);
        Debug.Log("Its working");


        this.rb.useGravity = true;

        //this.rb.isKinematic = true;
    }



}

【问题讨论】:

  • 代码似乎没问题。你检查碰撞是否有效吗?您也可以通过在 OnTriggerEnter() 中放置一个 Debug.Log 来做到这一点。
  • working 是什么意思?您是否尝试过debugging your code 以查看执行了哪些行?您的对象是否正确标记?另请注意:如果组件不是enabled,则不会执行协程,这可能是这种情况吗?
  • @KBaker 我放了 Debug.log,但是它不起作用,Yield return 下的 Debug.Log 之前可以工作,但现在它不能工作。
  • @derHugo 是的,我的对象标记正确。如何检查组件是否未启用?
  • 您必须检查场景中的对象是否将您的脚本作为组件以及左上角的蓝色勾号是否存在。

标签: c# visual-studio unity3d game-engine


【解决方案1】:

如果函数没有参数,则不需要使用 IEnumerable。您可以使用 Invoke("ObjectFall", 5) 并将您的 IEnumerable 转换为 void 而不会产生。它可能不是问题的根源,但请尝试一下。你的游戏中的规模可能是错误的,这就是质量 1000 有效的原因。尝试修复整个场景的比例。记住 1 个单位 = 或多或少 1 米

【讨论】:

    猜你喜欢
    • 2022-11-12
    • 2022-10-18
    • 2022-10-07
    • 2018-10-04
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2018-10-20
    • 2011-09-08
    相关资源
    最近更新 更多