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 Cyber-X-Zone · Aug 14, 2013 at 03:53 PM · instantiaterandomloops

For Loop isn't working properly!

I've made a script such that it instantiates an object randomly choosing between 2 objects. But, now the For loop doesn't work. Here's the script:

 var virusPrefab : Transform;
 var filePrefab : Transform;
 var position: Vector3;
 var virusesKilled : int = 0;
 var myResult : result;
 var randomVirus : float;
 var randomFiles : float;
 var randomTotal : float;
 var virusOrFile : float;
 
 function Start(){
     var myResultObject : GameObject = GameObject.FindGameObjectWithTag("Finish");
     if (myResultObject != null) {
             myResult = myResultObject.GetComponent(result);
     }
     randomTotal = Random.Range(45, 60);
     randomVirus = randomTotal - 25;
     randomFiles = randomTotal - randomVirus;
 }
 
 function Update(){
     position = Vector3(Random.Range(0.5, 2), Random.Range(-2, 2), 6.351526);
     
     myResult.total = randomTotal;
     myResult.totalVirus = randomVirus;
     myResult.score = virusesKilled;    
     myResult.totalFiles = randomFiles;
 }
  

 //The For loop section starts from here...

 yield WaitForSeconds(3);
 for (var i : int = 0;i < randomTotal; i++) {
     virusOrFile = Random.Range(0, 1);//To choose between the 2 objects
         
     if(virusOrFile == 0){
         var file : Transform = Instantiate(filePrefab, position, Quaternion.identity);
         file.transform.parent = GameObject.Find("files").transform;
     }    
     
     if(virusOrFile == 1){
         var virus : Transform = Instantiate(virusPrefab, position, Quaternion.identity);
         virus.transform.parent = GameObject.Find("viruses").transform;
     }
     yield WaitForSeconds(1);
 }

 //And ends here


 if(virusesKilled < 15){
     myResult.enabled = true;
     myResult.OnGUI();
     Application.LoadLevel("gameOver");
 }
 
 function AddScore(score : int){
     virusesKilled += score;
     Debug.Log("Score is : " + virusesKilled);
     return virusesKilled;
 }
 
 if(virusesKilled > 15){
     myResult.enabled = true;
     myResult.OnGUI();
 }

I decided to add all of the script here, if you see any variable problem.

Before, I have written the loop like this:

 yield WaitForSeconds(3);
 for (var i : int = 0;i < randomTotal; i++) {    
       var virus : Transform = Instantiate(virusPrefab, position, Quaternion.identity);
     virus.transform.parent = GameObject.Find("viruses").transform;
 
     yield WaitForSeconds(1);
 }

And it worked fine, but now after randomly choosing between two objects the loop isn't functioning!

Please help me...

Comment
Add comment · Show 1
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 meat5000 ♦ · Aug 14, 2013 at 04:08 PM 0
Share

robertbu is right. If you are using the 'int' version of random.range then the top number is never reached.

Check the page:

http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 14, 2013 at 04:01 PM

There are two forms of Random.Range(), one that takes integers, one that takes float. The integer version is exclusive of the top value. So 'Random.Range(0,1)' will only return 0. You need to use Random.Range(0,2) to get values 0 and 1.

You are unclear about what you mean by 'not working'. You should be executing the 'virusOrfile == 0' 'if' statement. If you are not, put Debug.Log() statements in the loop and inside each 'if' statement to see what is going on.

This code is also a good candidate for examining in the debugger. See this link for information on how to use the Mono debugger:

http://unitygems.com/debugging-game-monodevelop/

Comment
Add comment · Show 1 · 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
avatar image Cyber-X-Zone · Aug 14, 2013 at 04:09 PM 0
Share

Thanks, robertbu! It worked! Thank you... thank you....thank you very much. You helped me get out of this big mess.

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

16 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

Related Questions

How to instantiate 2D object at fixed location? 1 Answer

removing an int from a Random.Range 1 Answer

Spawn at random? 1 Answer

instantiating at a random time 1 Answer

Scripts of instantiated objects 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