我在 Unity 中有一个脚本,用于在我的赛车游戏中保存检查点。但我不知道赛道上有多少检查站。现在我仅限于 5 个:
但是,我想允许地图制作者插入更多的检查点。这不起作用:
public List<GameObject> checkPoints = new List<GameObject>();
那么最好的方法是什么?
定义检查点数组不起作用吗?
public GameObject[] CheckPoints;
我目前无法访问 Unity,但这应该允许您通过检查器将任何(合理)数量的 GameObjects 输入到 CheckPoints 数组中。
Unity Answers 上的这与此问题类似: http: //answers.unity3d.com/questions/245024/public-array-of-gameobjects.html
在这里胡乱猜测,是你的错误List1' could not be found
吗?在这种情况下,您必须添加using System.Collections.Generic;
才能使用 C# 列表。
This: public List<GameObject> checkPoints = new List<GameObject>();
doesn't show up because it's a list of GameObjects, not a list of Vectors. Users can't manually type in GameObjects. You could do: public List<Vector3> checkPoints = new List<Vector3>();
and then simply iterate through this list and do a Resource.Load for each position at runtime (if you can't figure this out, I could type up an example).
A more user friendly solution would be to allow users to add checkpoints by placing a GameObject in the world, rather than having to type each vector manually. So for instance, create a CheckPoint object, and give it a unique tag (eg: "CheckPointTag"), then simply run the method bellow to get all the checkpoint positions:
public List<Vector3> GetCheckPoints()
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("CheckPointTag");
List<Vector3> objectPos = new List<Vector3>();
foreach(GameObject object in objects)
objectPos.Add(object.transform.Position;
return objectPos;
}