Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by khoatic · Feb 08, 2014 at 08:54 AM · randomrandom.rangenumbergenerator

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);
     }
 }
Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Feb 08, 2014 at 08:55 AM 0
Share

What does 'size' evaluate to in debug

avatar image khoatic · Feb 08, 2014 at 08:57 AM 0
Share

my appologizes, the size is the value of the List size retrived earlier.

avatar image khoatic · Feb 08, 2014 at 08:58 AM 0
Share

added more of my code, to help clarify. CreateGrid is called before onTimer in the Update loop.

avatar image getyour411 · Feb 08, 2014 at 08:59 AM 1
Share

I get that, I meant literally what does size = in your debug.log, rigt after "SPAWN"

avatar image robertbu · Feb 08, 2014 at 09:00 AM 1
Share

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];
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges