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 ratboy · Apr 18, 2012 at 08:30 PM · instantiaterandomrandom.rangerepeat

Random.range multiple instantiations without repetition

Hello all, Ive been at this for the last day or so, and its starting to bug me - I have no problem instantiating several random game objects out of an array of multiple game objects, I can grasp the random.range with no problem, but when it comes to making sure the same game objects aren't instantiated more that once, I'm having difficulty, Ive tried a few scripts across the net but am having trouble.. Could anybody give me a hand?

Heres my code:

var bodyParts : GameObject[];

 function GenerateBodyParts() {
 var bodyPartsNum : int = Random.Range(0, bodyParts.length);
     for (var i = 0; i < 3; i++) {
         Instantiate(bodyParts[bodyPartsNum], transform.position + Vector3(0.05,0.05,-0.9), Quaternion.identity);
      }
  

}

I'd really appreciate some help guys, in JS if possible as I'm not very competent in any other language at the moment.

I know there are scripts out there but i can't seem to get them to work for this function, i.e. multiple random instantiations.

Cheers - monster.

Comment
Add comment
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

1 Reply

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

Answer by Lttldude · Apr 18, 2012 at 08:41 PM

Tested it and it works. Added a 1 second wait just so you could tell the order was random.

 var bodyParts : GameObject[];
 
 function Start()
 {
     GenerateBodyParts();
 }
 
 
 function GenerateBodyParts() {
     //make a copy of the original one
     //since we will be removing them from the array when we create them
     var bodyPartsCopy : GameObject[] = bodyParts;
 
     //loop through each bodypart
     for (var i = 0; i < bodyParts.Length; i++) {
     //pick a random index
     var bodyPartsNum : int = Random.Range(0, bodyPartsCopy.Length-1);
     //instantiate the bodypart
     yield WaitForSeconds(1);
     Instantiate(bodyPartsCopy[bodyPartsNum], transform.position + Vector3(0.05,0.05,-0.9), Quaternion.identity);
 
     //now need to remove the bodypart from the array so we don't create it again
     //but we need to make it a JS array first, remove it, then convert back to a builtin array to be used by unity
     var bodyPartsJS : Array = bodyPartsCopy;
     bodyPartsJS.RemoveAt(bodyPartsNum);
     bodyPartsCopy = bodyPartsJS;
 
     }
 }
Comment
Add comment · Show 7 · 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 ratboy · Apr 18, 2012 at 08:54 PM 0
Share

That looks like its headed in the right direction, thanks for the quick response - but I'm getting the following errors:

$$anonymous$$ identifier: 'count'. RemoveAt is not a member of UnityEngine.GameObject[] ToBuiltin is not a member of UnityEngine.GameObject[]

any ideas? thanks again.

avatar image Lttldude · Apr 18, 2012 at 09:23 PM 0
Share

I got rid of the whole count variable. I figured out you didnt need it. Hope it works.

avatar image ratboy · Apr 18, 2012 at 10:05 PM 0
Share

yeah of course, its already in the bodyPartsNum.. any ideas to the ToBuiltIn and RemoveAt not being members of UnityEngine.GameObject[] ? cheers!

avatar image Lttldude · Apr 18, 2012 at 11:37 PM 0
Share

Updated my answer, all works.

avatar image ratboy · Apr 19, 2012 at 02:03 AM 0
Share

Will test in the morning, and will let you know then - thanks for the quick reply! :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I made this script to instantiate objects, but when the game starts I get this error:IndexOutOfRangeException: Array index is out of range. 1 Answer

Randomizing a selection from an array 1 Answer

Trying to avoid repeating elements on list (random.range) 1 Answer

Calling 2 specific random float numbers in random.range using array not working! 2 Answers

Instantiate issues 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