- Home /
The question is answered, right answer was accepted
Random.Range with Lists problem.
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.
This can happen if you add some elements in vectorPositionList between two calls of your Random.Range line.
I don't seem to be able to understand what you are saying. :(
Answer by ObeliskEntertainment · May 27, 2014 at 05:38 AM
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.
for posting solution.
Glad I could help. Not sure what the error with .Next() was, because I can't recreate it, but, whatever :P
Answer by Chris_Dlala · May 26, 2014 at 01:44 PM
You are using the combination of Random.Range
and 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 =)
Answer by tw1st3d · May 26, 2014 at 01:17 PM
using UnityEngine;
using System;
using System.Collections;
public class Vector3Test : MonoBehavior
{
private Vector3[] vectors = new Vector3[4];
private Vector3 selected;
public void Awake()
{
// Create your four vectors
this.vectors[0] = new Vector3(0, 0, 1, 1);
this.vectors[1] = new Vector3(1, 1, 0, 0);
this.vectors[2] = new Vector3(1, 1, 1, 1);
this.vectors[3] = 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
Debug.Log(this.selected.ToString("F3"));
}
}
Also, directly to your code error,
Vector3 position = vectorPositionList [Random.Range (0, vectorPositionList.Count) ];
should read
Vector3 position = vectorPositionList[ Random.Range(0, (vectorPositionList.Count - 1)) ];
In your examples you will never get the last entry in the list/array because the second parameter in Unity's Random.Range and System.Random.Next are both exclusive.
All looks good except for .Next throws me an error.
UnityEngine.Random does not contain a definition for Next
and by adding using Systems; It makes Random throw me an error as well.
Follow this Question
Related Questions
Why multiply a vector by -2? 1 Answer
How to get an offset of a RaycastHit? 0 Answers
Get the position of the first finger that touched the collider? 1 Answer
Moving a 3D object with 2D swipes 2 Answers
raycast and trigonometry 2 Answers