【发布时间】: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