How to make an array that stores x, y and z co-ordinates?
Currently making a game that will spawn zombies at a random, although predetermined, location. In order to do this I need to store these locations in some kind of way. it was recommended to me that I should store this in an array and randomly select on of those positions from the array/list.
How do I go about writing an array for co-ordinates and also randomly selecting one?
FYI I am relatively new to C# and also to Unity.
Answer by InfiniBuzz · Oct 20, 2016 at 09:17 AM
You don't need to store x,y,z coordinates seperatly unless you explicitly aim for low memory/high performance. You can create Lists/Arrays of type Vector3. IF you want to set the positions from the inspector you need to add the array/list to your script and make it public
If you want to use an Array:
Vector3[] positions = new Vector3[] {
new Vector3(),
new Vector3(1,1,1)
};
// OR
Vector3[] positions = new Vector3[10]; // create array
positions[0] = new Vector3();
positions[1] = new Vector3(1,1,1);
// etc
...
// access array
Debug.Log(positions[0].ToString());
if you want to use lists:
List<Vector3> positions = new List<Vector3>();
positions.Add(new Vector3());
Debug.Log(positions[0].ToString());
Inspector access:
public class YourClass : MonoBehaviour {
public List<Vector3> MyPositions = new List<Vector3>();
void Start()
{
}
// etc...
....
}