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*;*
*/
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);
}
}
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.