- Home /
Random.Range(..) not working
So Im trying to generate a random number from the size of a List. I checked to make sure my function was obtainning to Count of the List, however the Random Generator just doesn't want to give me a value. Please help.
private int size;
public GameObject Node;
private List<GameObject> NodeList;
public void createNodeGrid()
{
for(int i=0;i<=2000;i+=100)
{
for(int k=0;k<=2000;k+=100)
{
GameObject tmp;
tmp= Instantiate(Node, new Vector3(i, 20, k), Quaternion.identity) as GameObject;
NodeList.Add(tmp);
}
}
size = NodeList.Count;
}
void onTimer(object source, ElapsedEventArgs e)
{
Debug.Log("TIME");
Debug.Log(size);
if (curSmokeMState==SmokeMoverState.DEAD)
{
Debug.Log("SPAWN");
Debug.Log(size);
/*
int i = Random.Range(0, size);
Debug.Log(i);
GameObject spawnNode = NodeList[i];
*/
GameObject spawnNode = NodeList[Random.Range(0, size)];
Debug.Log(spawnNode.transform.position.x);
smokeMover.transform.position = new Vector3(spawnNode.transform.position.x, spawnNode.transform.position.y, spawnNode.transform.position.z);
}
}
my appologizes, the size is the value of the List size retrived earlier.
added more of my code, to help clarify. CreateGrid is called before onTimer in the Update loop.
I get that, I meant literally what does size = in your debug.log, rigt after "SPAWN"
I doubt it is the Random.Range() that is failing. Best guess is that the problem is with your initialization of NodeList. To test, break your code down:
int i = Random.Range(0, size);
Debug.Log(i);
GameObject.spawnNode = NodeList[i];
Answer by unimechanic · Feb 24, 2014 at 09:14 PM
You are not providing enough code to determine the problem. I modified what you posted in order to execute and test:
using UnityEngine;
using System.Collections.Generic;
using System.Timers;
public class RandomGenerator: MonoBehaviour
{
enum SmokeMoverState
{
DEAD,
}
public GameObject Node;
private int size;
private List<GameObject> NodeList;
private Timer timer;
private SmokeMoverState curSmokeMState;
void Start()
{
// 1. Make sure to set this variable to DEAD so the spawn code will get called:
curSmokeMState = SmokeMoverState.DEAD;
// 2. Make sure to initialize the grid:
NodeList = new List<GameObject>();
createNodeGrid();
// 3. Now you can create the timer and it's event:
timer = new Timer(1);
timer.Elapsed += onTimer;
timer.Start();
}
void Update()
{
}
public void createNodeGrid()
{
for(int i=0;i<=2000;i+=100)
{
for(int k=0;k<=2000;k+=100)
{
GameObject tmp;
tmp= Instantiate(Node, new Vector3(i, 20, k), Quaternion.identity) as GameObject;
NodeList.Add(tmp);
}
}
size = NodeList.Count;
}
void onTimer(object source, ElapsedEventArgs e)
{
Debug.Log("TIME");
Debug.Log(size);
if (curSmokeMState==SmokeMoverState.DEAD)
{
int index = Random.Range(0, size);
Debug.Log("SPAWN");
Debug.Log(size);
GameObject spawnNode = NodeList[index];
Debug.Log(spawnNode.transform.position.x);
// smokeMover.transform.position = new Vector3(spawnNode.transform.position.x, spawnNode.transform.position.y, spawnNode.transform.position.z);
}
}
}
And it throws the following error:
RandomRangeInt can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
That means the Timer class is calling the event from another thread, raising an exception when generating the random number. Use a different way of creating your timer functionality, for example calculate the time elapsed in Update to call that event.
Your answer
Follow this Question
Related Questions
RNG output to a variable + instantiating a prefab based on said variable? 1 Answer
Random texture changer (problem with array) 2 Answers
Randomly generated number 0 Answers
ArgumentException: RandomRangeInt can only be called from the main thread. 1 Answer
I don't understand the random number generator output. 1 Answer