我有一个带盒对撞机(触发器)的空对象,在这个空对象内,我有一些具有相同标签的多维数据集,因此当所有这些多维数据集都变成空对象之外时,我想实例化文本(使用OnTriggerExit )。
这是我尝试的代码,但是没有用:
List<string> contacts = new List<string>();
void OnCollisionExit(Collision col)
{
contacts.Add(col.gameObject.tag);
if (contacts.Contains("Cube"))
{
GameObject canvas = GameObject.Find("Canvas");
Text text = Instantiate(Text, new Vector3(562, 1800, 0), Quaternion.identity);
text.transform.SetParent(canvas.transform);
}
}
答案 0 :(得分:0)
跟踪触发器中有多少个多维数据集。当标签为“ Cube”时,在场景开始时进行设置,使用OnTriggerEnter递增,使用OnTriggerExit递减。一旦达到零,就调用您的函数。
// Inside of MonoBehavior on the GameObject with the trigger.
public int cubesCurrentlyInTrigger;
void Start()
{
cubesCurrentlyInTrigger = 3; // Or however many cubes at start
}
void OnTriggerEnter(Collider col)
{
// check for entering cubes
if (col.tag == "Cube")
{
cubesCurrentlyInTrigger++;
}
}
void OnTriggerExit(Collider col)
{
// check for exiting cubes
if (col.tag == "Cube")
{
cubesCurrentlyInTrigger--; // Could use `--cubesInTrigger` inside the if,
// but this is more readable
if (cubesCurrentlyInTrigger == 0)
{
DoFunction(); // Put your function here.
}
}
}