C# - Unity crashes with this code - creating Transform array based on GameObject tag
我在一个三维环境中工作,一个有16个房间的房子。在每个房间里,我都放置了一个不可见的立方体,上面有一个名为"roomstoscantag"的标签。我正在使用一个名为行为设计器的资产,在我的检查器中,我已经给出了要查找的标记名(在本例中是"roomstoscantag")。使用此标记名,我希望机器人先转到Room1,然后继续转到Room2,直到Room16。
这是我的密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using UnityEngine; using BehaviorDesigner.Runtime; using BehaviorDesigner.Runtime.Tasks; public class MoveTowardsNew : Action { private Transform[] roomsToScanHAHA; public string targetTagRooms; public Transform targetyo; int i = 0; int controlNumber = 0; public override void OnAwake() { var targets = GameObject.FindGameObjectsWithTag (targetTagRooms); roomsToScanHAHA = new Transform[targets.Length]; for (int i = 0; i < targets.Length; i++) { roomsToScanHAHA [i] = targets [i].transform; } } public override TaskStatus OnUpdate() { while (controlNumber < roomsToScanHAHA.Length) { targetyo = roomsToScanHAHA [controlNumber]; if (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f) { if (controlNumber < roomsToScanHAHA.Length) { controlNumber++; } return TaskStatus.Success; } } NavMeshAgent agent = GetComponent<NavMeshAgent> (); agent.destination = targetyo.position; return TaskStatus.Running; } } |
我的问题
统一崩溃…这与这段代码有关,我想不出来。有人知道怎么回事吗?
以下是我正在处理的问题的图像:
事先谢谢!
与往常一样,它的
1 2 3 4 5 6 7 8 9 10 11 | while (controlNumber < roomsToScanHAHA.Length) { targetyo = roomsToScanHAHA [controlNumber]; if (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f) { if (controlNumber < roomsToScanHAHA.Length) { controlNumber++; } return TaskStatus.Success; } } |
第一次运行:
假设数组
因此,您可以将代码更改为如下内容:
1 2 3 4 5 6 7 8 9 | while (controlNumber < roomsToScanHAHA.Length) { targetyo = roomsToScanHAHA [controlNumber]; if (Vector3.SqrMagnitude (transform.position - targetyo.position) < 0.5f) { return TaskStatus.Success; } controlNumber++; } |
希望这有帮助