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 coolbird22 · Apr 13, 2014 at 06:15 PM · instantiatelistmultipleclone

How to add multiple clones of an object to a list ? Currently only 1 clone gets added.

Only 1 of the multiple instantiated clones of an object is getting added to my list, instead of ALL of them. Also, is it possible to store all such clones under a single variable so I can access all of them from a different script ??

Here is my code:

     public GameObject[] enemies;        // Array of enemy prefabs.
     List<GameObject> instantiatedCrowd;
     public GameObject instantiatedEnemies;

     void Start ()
     {
         // Start calling the Spawn function repeatedly after a delay .
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     void Spawn ()
     {
         // Instantiate a random enemy.
         int enemyIndex = Random.Range(0, enemies.Length);
         instantiatedEnemies = Instantiate(enemies[enemyIndex], transform.position, transform.rotation) as GameObject;
         instantiatedCrowd.Add (instantiatedEnemies.gameObject);
     }
Comment
Add comment · Show 15
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 Malfegor · Apr 13, 2014 at 06:24 PM 0
Share

How and where do you call the Spawn() function? If it only gets called once, it's just one random enemy spawning, right? Also if you want to loop through the whole enemy array, for a Random.Range based on integers does not include the 'max' property. So Random.Range(0, 5) returns a number between 0 and 4 (included) not 0 and 5.

avatar image coolbird22 · Apr 13, 2014 at 06:29 PM 0
Share

Spawn() gets called under InvokeRepeating every few seconds. Enemy array has different types of enemies that spawn. Random.Range of 0 to enemies.length is basically asking it to choose a random item from the entire array, and has nothing to do with the list that I want each and every spawned enemy to get listed under.

avatar image coolbird22 · Apr 13, 2014 at 08:40 PM 0
Share

$$anonymous$$anaged to get it to work with the following script, but it updates the list only when the Invoke is called and not every frame, as I would want it to be. I can't add the Instantiate command under Update() since I'm making a call to the Spawn() under the Invoke. Any ideas ?

     public GameObject[] enemies;        // Array of enemy prefabs.
     public List <GameObject> instantiatedCrowd;
 
 
     void Start ()
     {
         // Start calling the Spawn function repeatedly after a delay .
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     
     void Spawn ()
     {
         // Instantiate a random enemy.
         int enemyIndex = Random.Range(0, enemies.Length);
         instantiatedCrowd.Add(Instantiate(enemies[enemyIndex], transform.position, transform.rotation) as GameObject);
 
     }    
 
avatar image neonblitzer · Apr 14, 2014 at 12:34 AM 0
Share

Why would you want the list to be updated every frame? If enemies are added only in Spawn(), isn't it enough that the list is updated there? What do you want to do with the list instantiatedCrowd?

avatar image coolbird22 · Apr 14, 2014 at 02:16 AM 0
Share

I wish to check for bounds of all the objects inside the list so that I can check if those objects are intersecting with a different object, so as to trigger damage to player health.

Show more comments

1 Reply

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

Answer by coolbird22 · Apr 28, 2014 at 03:58 PM

Marking this solved. Answer includes most of the replies, with Benproductions1 answering my final query

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

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does instantiation of clone cause duplicates 1 Answer

A node in a childnode? 1 Answer

Why are multiple objects being instantiated when I use clone, but not regular instantiation? 1 Answer

Cloning Problem Script 0 Answers

Instantiate random from a list gives error: "Reference not set to an instance" 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