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 Yanneyanen · Oct 08, 2013 at 03:58 PM · instantiatearrayrandomrandomizationshuffle

How to instantiate the first 8 gameobjects in an array (UnityScript)?

Hi, I'm making a simple random generation script. Right now the script has 2 arrays: one for the objects it should spawn and another for the spawn locations. I've made it so that at the start of the level, the script shuffles the "objects to spawn" array, so the order is randomized. Then the script should spawn the objects (rooms) to the spawnpoints.

The thing I can't figure out is how to spawn (instantiate) just the first 8 gameobjects of the shuffled array. Right now the SpawnRooms function spawns random objects between 0 and 7 in the array until the spawn points are filled, but what I'd want for it to do is spawn the first 8 in the array (or element 0, 1, 2, 3, 4, 5, 6 and 7) The goal is to never spawn the same room twice, which would happen with shuffle array -> spawn the first 8 objects.

my code:

 var objectsToSpawn = new GameObject[12];
 var spawnLocations = new Transform[8];
  
 function Start(){
     Shuffle();
     SpawnRooms();
 }
 
 
 function Shuffle() {
     for (i = 0; i < objectsToSpawn.Length; i++) {
         var temp = objectsToSpawn[i];
         var randomIndex = Random.Range(0, objectsToSpawn.Length);
         objectsToSpawn[i] = objectsToSpawn[randomIndex];
         objectsToSpawn[randomIndex] = temp;
     }
 }
 
 
 function SpawnRooms(){
 
   for (var i=0; i < spawnLocations.length; i++){
 
     var thingToSpawn : int = Random.Range( 0, 7);
     
     Instantiate( objectsToSpawn[thingToSpawn], spawnLocations[i].position, transform.rotation );
 
 }
 }
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
1

Answer by robertbu · Oct 08, 2013 at 04:05 PM

Since you are shuffling the array, you don't need to generate random values in this funciton. Try:

 function SpawnRooms(){
  
   for (var i=0; i < spawnLocations.length; i++){
     Instantiate( objectsToSpawn[i], spawnLocations[i].position, transform.rotation 
     }
 }
Comment
Add comment · Show 4 · 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 Yanneyanen · Oct 08, 2013 at 04:14 PM 0
Share

Of course! I knew it was something this simple, I'm just not much of a programmer... Anyway, it works, so thank you!

avatar image vinnymc · Nov 16, 2013 at 10:42 PM 0
Share

Thanks much for this answer. Stumbled and bumbled my way around trying to do this all afternoon and finally found this thread.

avatar image vinnymc · Nov 16, 2013 at 11:32 PM 0
Share

Also a note the last line above should read

Instantiate( objectsToSpawn[i], spawnLocations[i].position, spawnLocations[i].rotation);

assu$$anonymous$$g you want the spawned object's rotation to match that of the selected spawnlocation

avatar image vinnymc · Nov 17, 2013 at 12:09 AM 0
Share

Since i Found this all so helpful one last addition to the SpawnRooms function -


function SpawnRooms(){

 for (var i=0; i < spawnLocations.length; i++){
 

var spawnedobject = Instantiate( objectsToSpawn[i], spawnLocations[i].position, spawnLocations[i].rotation); // this line creates a new variable and sets it to the Instantiated object

spawnedobject.transform.parent = spawnLocations[i].transform; // this line then sets the parent of the Instantiated object to that of the SpawnLocation object

 }
 }


This way if one of your SpawnLocations is parented to some other object or even a bone in an animated rig - the spawned object will maintain the parenting relationship of the SpawnLocation. Nice !!!

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

Instantiate Prefab at random times but keep 3 from spawning 2 Answers

instantiate a random object from multiples via 'tag' 1 Answer

RandFuncs.Shuffle question 0 Answers

multiple object in multiple spawn Point with out repeat 0 Answers

Randomly instantiate objects from array without choosing the same item twice. 2 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