【发布时间】:2021-02-16 07:09:56
【问题描述】:
我有一张由 6 个带有标签 LevelBlock 的块组成的地图。汽车离开当前所在的街区后,我想删除该街区。现在我的代码会删除随机的 LevelBlock,但不会删除汽车之前所在的那个。如何删除不再使用的LevelBlock?
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Diagnostics;
public class TriggerExit : MonoBehaviour {
public float delay = 5f;
public delegate void ExitAction();
public static event ExitAction OnChunkExited;
private bool exited = false;
private void OnTriggerExit(Collider other)
{
CarTag carTag = other.GetComponent<CarTag>();
if (carTag != null)
{
if (!exited)
{
exited = true;
OnChunkExited();
StartCoroutine(WaitAndDestroy());
}
}
}
IEnumerator WaitAndDestroy()
{
yield return new WaitForSeconds(delay);
// Destroy the LevelBlock
var block = GameObject.FindWithTag("LevelBlock");
Destroy(block);
}
}
【问题讨论】:
标签: c# unity3d game-development destroy