I have updated my question as I have narrowed out my problem. I can’t seem to be able to store a TOTAL of 4 Vector3 Positions and use Random.Range to randomly select one of them.
I previously used the Vector3 position = vectorPositionList [Random.Range (0, vectorPositionList.Count) ];
It worked sort of. When I ran the script more times than the amount of Vector3’s I had stored in the List I got an “Out of Range” error. It needs to be able to run in a loop somehow.
You are using the combination of
List.Count correctly. When passed two integers you will get a random number between the min and max but never max because the max is exclusive. However, if you have a list with a count of 0 (an empty list), you will get an error:
List<Vector3> list = new List<Vector3>(); // An empty list
int index = Random.Range(0,list.Count); // Count will be 0
Vector3 v = list[index]; // This error
This code will throw an OutOfRangeException because count is 0 and there is no entry at index 0. To avoid this you will need to check if the list is empty (eg list.Count == 0) before getting a random entry. I hope that helps =)
I solved the problem myself. I used the typical Random.Range to create a number, and inserting it directly into an equation like this.
int randomGenerated = Random.Range (0,4);
this.selected = this.vectors [randomGenerated];
Gets the job done well, looks clear and is short. I love it! I used tw1st3d 's idea and tweaked it to make it work properly. The “.NEXT” method did not work, it threw out an error.
public class Vector3Test : MonoBehavior
private Vector3 vectors = new Vector3;
private Vector3 selected;
public void Awake()
// Create your four vectors
this.vectors = new Vector3(0, 0, 1, 1);
this.vectors = new Vector3(1, 1, 0, 0);
this.vectors = new Vector3(1, 1, 1, 1);
this.vectors = new Vector3(0, 0, 0, 0);
public void Start()
// Generate random number
Random rnd = new Random();
int picked = rnd.Next(0, 3);
// Use random number to pick the vector3
this.selected = this.vectors[picked];
// Log the randomly picked vector