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 Zearla · Sep 04, 2013 at 06:05 AM · random.range

How to stop multiples of game objects instantiating when using random.range with an array

I am new to scripting and I find it very hard to grasp. I have a project for an internship for my game design skills but this also means I have to do some scripting to complete certain tasks. I have the grasp of most things I need but arrays and loops are not my friends. This is my problem. I instantiate six objects randomly from an array of game objects. This works fine but I frequently get the same object instantiated twice or more. I have searched all the questions in unity and Googled all sorts of word combinations but they all seem to involve integers and numbers printed out not actual game objects.I know I have to use a for loop but I'm not sure where to start. This is the section of code I need to add to I know but I'm not quite sure how.

 function LeftChoices()
 {
     objArray = new GameObject[6];
     
     for (var i : int = 0 ; i < 6 ; i++)
     {
         objArray[i] = Instantiate(leftObjArray[Random.Range(0,leftObjArray.Length)], posArray[i] , Quaternion.identity);
     }
 }

If someone could give me a hand that would be absolutely awesome. Many thanks in advance.

Comment
Add comment · Show 3
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 · Sep 04, 2013 at 06:24 AM 0
Share

$$anonymous$$ove Random.Range outside of Instantiate(), make it var myNum = Random.Range, then check to see if GameObj[myNum] is already in objArray.

avatar image robhuhn · Sep 04, 2013 at 07:30 AM 0
Share
  • So you always want to instantiate six objects irrespective of the array size?

  • You don't want to get the same random twice (like 1,2,1) or twice in a row (like 1,1)?

avatar image Zearla · Sep 04, 2013 at 07:51 AM 0
Share

In order of responses; Response 1: I'm not sure how that would work as it's the instantiate I'm applying the random.range to, could you please explain in a bit more detail. Response 2: Yes I always want to instantiate 6 objects and I don't want to get the same random at all (like 1,6,4,21,19,8). The six objects are chosen from an array of 21 objects.

1 Reply

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

Answer by robhuhn · Sep 04, 2013 at 08:45 AM

I made an example where a function GetUniqueRandom stores all previous random numbers in a list and returns the new unique number:

 var rndsList : List.<int> = new List.<int>();

 function GetUniqueRandom(min : int, max : int) : int
 {
     var rnd : int; //define an int to store the rnd number
     do //the do-while loop will perform one loop at minimum and further loops if the while-condition is met 
     {
         rnd = Random.Range(min, max); //generate a random
     }while(rndsList.IndexOf(rnd) != -1); //run another loop (generate a new number) if the list already contains this number

     rndsList.Add(rnd); //store the valid number

     return rnd; //return the number
 }     

Then call the method from LeftChoices

 for (var i : int = 0 ; i < 6 ; i++)
 {
    objArray[i] = Instantiate(leftObjArray[GetUniqueRandom(0,leftObjArray.Length)], posArray[i] , Quaternion.identity);
 }

I'm not used to UnityScript so there might be a shorter way.

Comment
Add comment · Show 13 · 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 ArkaneX · Sep 04, 2013 at 09:01 AM 0
Share

This requires adding

 import System.Collections.Generic;

at the beginning of the script.

avatar image ArkaneX · Sep 04, 2013 at 09:30 AM 1
Share

@robhuhn answer is good enough, but I would slightly change it. Ins$$anonymous$$d of retrying generation of random number if it is already in the list, below code simply adds 1 to it or reverts to $$anonymous$$ in case when max is reached:

 function GetUniqueRandom($$anonymous$$ : int, max : int) : int
 {
     var rnd : int = Random.Range($$anonymous$$, max);
     while (rndsList.Contains(rnd))
     {
         rnd = rnd == max - 1 ? $$anonymous$$ : rnd + 1;
     }
     rndsList.Add(rnd);
  
     return rnd;
 }
avatar image Zearla · Sep 05, 2013 at 02:33 AM 0
Share

Thanks for the responses I thought I would try robhuhns answer first. I tried this and when I saved I got this warning.

 Assets/Scripts/ObjectChoices.js(53,10): BCW0023: WARNING: This method could return default value implicitly. 

This is the line it was referring to

 function GetUniqueRandom($$anonymous$$ : int, max : int) : int

Then when I ran it I continued to get duplicates and I got this warning

 Assets/Scripts/ObjectChoices.js(63,16): BCW0015: WARNING: Unreachable code detected.

This is the line it was referring to

    LeftChoices();

I don't know why it says this line is unreachable code. This is the whole script as it is now with the warnings.

 #pragma strict
 import System.Collections.Generic;
 
 var rndsList : List.<int> = new List.<int>();
 var leftObjArray : GameObject[];
 var rightObjArray : GameObject[];  
 var posArray : Vector3[];
 var objArray : GameObject[];
 var posArray1 : Vector3[];
 var objArray1 : GameObject[];
 var Texture1 : Texture;
 var TextureButton: Texture;
 
 function Start ()
 {
     LeftChoices();
     RightChoices();
 }
 
 function GetUniqueRandom($$anonymous$$ : int, max : int) : int
 {
     var rnd : int; 
     do  
     {
         rnd = Random.Range($$anonymous$$, max); 
     }
     while(rndsList.IndexOf(rnd) != -1); 
     rndsList.Add(rnd);
     return rnd;
     LeftChoices();
      
 }
 
 function LeftChoices()
 {
     objArray = new GameObject[6];
     
     for (var i : int = 0 ; i < 6 ; i++)
     {
         objArray[i] = Instantiate(leftObjArray[Random.Range(0,leftObjArray.Length)], posArray[i] , Quaternion.identity);
     }
 }
 
 function RightChoices()
 {
     objArray1 = new GameObject[4];
     
     for (var f : int = 0 ; f < 4 ; f++)
     {
         objArray1[f] = Instantiate(rightObjArray[Random.Range(0,rightObjArray.Length)], posArray1[f] , Quaternion.identity);
     }
 }
 
 function OnGUI()
 {
     if(GUI.Button(Rect((Screen.width/2)-300,500,50,50), TextureButton,"Label"))
     {
 
         //objArray = null;
 
         //print("ta da" );
         LeftChoices();
     }
 }

The objArray = null line is where I'm trying to remove the items from the objarray and put new objects in.

If anyone can tell me what I'm doing wrong with this script it would be most appreciated. I'm completely useless when it comes to scripting.

avatar image Zearla Zearla · Sep 05, 2013 at 02:38 AM 0
Share

I tried ArkaneXs solution and the warnings cleared up but I still have duplicates of game objects instantiating. I know it's probably something simple but I'm getting frustrated with this not working.

avatar image Zearla Zearla · Sep 05, 2013 at 02:59 AM 0
Share

I have just saved a different script and the above warnings are back could someone please explain what they mean.

avatar image robhuhn · Sep 05, 2013 at 06:39 AM 0
Share

The unreachable code in your script is here:

 function GetUniqueRandom($$anonymous$$ : int, max : int) : int
 {
     var rnd : int; 
     do  
     {
         rnd = Random.Range($$anonymous$$, max); 
     }
     while(rndsList.IndexOf(rnd) != -1); 
     rndsList.Add(rnd);
     return rnd; //when the method has something returned it is done and nothing below will be executed
     LeftChoices(); //unreachable code - delete this line
 }

And you didn't change this

 objArray[i] = Instantiate(leftObjArray[Random.Range(0,leftObjArray.Length)], posArray[i] , Quaternion.identity);

to this

 objArray[i] = Instantiate(leftObjArray[GetUniqueRandom(0,leftObjArray.Length)], posArray[i] , Quaternion.identity);

So GetUniqueRandom is never called.

avatar image Zearla · Sep 05, 2013 at 07:04 AM 0
Share

Cool thankyou that solved the issue I thought I had to call the left choices function and now I have no duplicates now I'm actually calling the random function. Thankyou so much.

Show more comments

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

18 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

Related Questions

Random.Range Spawner 1 Answer

Generate random number per function call? 1 Answer

Have random.range anims access float speed variable 0 Answers

Color GUILayout Buttons from Array of Colors (C#) 2 Answers

randomize a function from a array (C#) 3 Answers


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