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 /
This question was closed Jul 22, 2013 at 03:56 PM by MadJohny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MadJohny · Mar 24, 2013 at 02:14 PM · javascriptarrayarrays

Select randomly x arrays and instantiate them

I created an array with 135 GameObjects, now, from that 135 GameObjects and from that 135 I want to randomly select 85 and spawn them, but how do I make to randomly choose 85 and spawn them |!!!I don't want to spawn 2 times the same GameObject!!!| <-- this is for real, I really don't want to have repeated GameObjects because the GameObjects are the same the only thing that changes is the position. If you know how to do it please comment my script edited by you if you don't mind. thanks in advance. #pragma strict

 var bricks : GameObject [];
 
 function Awake () 
 {
     GameObject.Instantiate(bricks[Random.Range(0,bricks.Length)], transform.position, transform.rotation);
 }
 
 function Update () {
 
 }
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 AlucardJay · Mar 24, 2013 at 02:13 PM 0
Share

Easiest way would be to create and then store all your gameObjects in a List, and then simply use RemoveAt(index)

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

scroll down to Generic List

avatar image MadJohny · Mar 24, 2013 at 02:19 PM 0
Share

thanks must try that. edit: I have no ideia on what to do

avatar image AlucardJay · Mar 24, 2013 at 02:32 PM 0
Share

I'm writing an answer now, give me a moment =]

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Mar 24, 2013 at 02:43 PM

List seems difficult to use, but really it is easy and very flexible once you do some practicing with it. Please read my script carefully, follow the comments, cross-reference with the link I provided, and see if you do understand what is going on :

 #pragma strict
 
 // to use List in uJS
 import System.Collections.Generic;
 // --
 
 
 var bricksToSpawn : int = 85;
 
 var bricks : GameObject [];
 
 var bricksList : List.< GameObject >; // declare a new list
 
 
 function Start() 
 {
     PopulateBricksList();
     
     ChooseRandomBricks();
 }
 
 
 function PopulateBricksList() 
 {
     // declare a new list
     bricksList = new List.< GameObject >();
     
     // get the length of the built-in array
     var totalBricks : int = bricks.Length;
     
     // add each brick to the brickList
     for ( var i : int = 0; i < totalBricks; i ++ )
     {
         bricksList.Add( bricks[i] );
     }
 }
 
 
 function ChooseRandomBricks() 
 {
     // choose a brick from the bricksList
     for ( var i : int = 0; i < bricksToSpawn; i ++ )
     {
         // find the current length of the bricksList
         var bricksRemaining : int = bricksList.Count;
         
         // get a random number
         var rndChoice : int = Random.Range( 0, bricksRemaining - 1 );
         
         // instantiate that chosen brick
         var cloneBrick : GameObject = Instantiate( bricksList[ rndChoice ], transform.position, transform.rotation );
         
         // remove that brick from the list
         bricksList.RemoveAt( rndChoice );
     }
 }

That link again : http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

scroll down to Generic List

Comment
Add comment · Show 11 · 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 MadJohny · Mar 24, 2013 at 02:48 PM 0
Share

Ok thanks alot I'm going to try it now.

avatar image MadJohny · Mar 24, 2013 at 03:01 PM 0
Share

Ok it definitly works, I'm gonna study a little bit and then I will record an update video about my game and obviously I will refer you in description

avatar image AlucardJay · Mar 24, 2013 at 03:03 PM 0
Share

Excellent ! Don't forget to accept any answer you are happy with =]

avatar image AlucardJay · Mar 24, 2013 at 03:05 PM 0
Share

Here is my notepad of List notes, the things that get used the most :

 // to use List
 import System.Collections.Generic;
 
 // declare
 var inRangeList : List.< GameObject > = new List.< GameObject >();
 
 var inRangeList : List.< GameObject >;
 inRangeList = new List.< GameObject >();
 
 // Add object to list :  
 inRangeList.Add( theItem );
 
 // length
 inRangeList.Count;
 
 // read from position 
 inRangeList[ index ];
 
 // empty list
 inRangeList.Clear();
 
 // send to built-in array
 inRangeList.ToArray();
avatar image MadJohny · Mar 24, 2013 at 04:02 PM 1
Share

ok, if you are curious: http://www.youtube.com/watch?v=rDg-H$$anonymous$$I0F38&feature=youtu.be

Show more comments

Follow this Question

Answers Answers and Comments

11 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

Related Questions

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

How to create an auido array (JavaScript) 1 Answer

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

Random an Array to push to another Array 0 Answers

Javascript array questions. 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