我只是想砍倒一棵樹并重新生成它。我這樣做只是通過使用計時器使樹預制件處于非活動狀態然后處于活動狀態。
樹將在 1 秒(第一個等待計時器)后變為非活動狀態,然后在 2(第二個等待計時器)后變為活動狀態。
然而,奇怪的是,第二個等待計時器似乎不起作用,如下面的日志所示。任何幫助都會很棒。
這是我的 ChopTree 腳本:
public class ChopTree : MonoBehaviour
{
private bool touching;
private void OnTriggerEnter(Collider other)
{
Debug.Log("Touching tree");
touching = true;
Debug.Log(touching);
}
private void OnTriggerExit(Collider other)
{
Debug.Log("Not Touching tree");
touching = false;
Debug.Log(touching);
}
private void Update()
{
if (Keyboard.current.enterKey.wasPressedThisFrame)
{
if (touching == true)
{
StartCoroutine(TreeSpawner.DeleteAndRespawn());
}
}
}
}
這是樹生成器腳本:
public class TreeSpawner : MonoBehaviour
{
// Assign tree prefab.
[SerializeField]
private GameObject tree;
// Access-point for tree script.
private static TreeSpawner instance;
// Spawn the tree when the game Starts.
void Start()
{
instance = this;
}
IEnumerator Respawn(float timeToDespawn, float timeToRespawn)
{
yield return new WaitForSeconds(timeToDespawn);
Debug.Log("Deleting...");
tree.SetActive(false);
Debug.Log("Deleted...");
Debug.Log("Respawning...");
yield return new WaitForSeconds(timeToRespawn);
Debug.Log("Respawn Timer done...");
tree.SetActive(true);
Debug.Log("Respawned...");
}
public static IEnumerator DeleteAndRespawn()
{
Debug.Log("Deleting and respawning tree");
yield return instance.StartCoroutine(instance.Respawn(1f, 2f));
}
}
以下是日志的順序:

如您所見,日志中說重生計時器已完成從未發生過。這是為什么?
uj5u.com熱心網友回復:
我猜您已經將“樹”欄位設定為放置了 TreeSpawner 腳本的游戲物件。當“樹”游戲物件被停用時......
tree.SetActive(false);
...然后 TreeSpawner 腳本也會停止。
當使用 SetActive(false) 禁用它所附加的 GameObject 時,協程也會停止
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/322897.html
標籤:统一3d
