Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dec 02, 2013 at 04:29 AM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by W1k3 · Sep 29, 2013 at 03:21 AM · instantiateparentchildfor looploops

[C#] How do I instantiate multiple objects at the same time?

I'm instantiating two capsules with force. When I make one a prefab that has the other as a child, One of the capsules sort of shakes on the spot while the other shoots forward.

I read on another thread that I should use a for loop. If that's the best solution, could you show me how you would use it?

Comment
Add comment · Show 1
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 vexe · Oct 06, 2013 at 05:38 PM 0
Share

DEAR @W1k3: Is there anything else related to we could do for you? - If so, please tell us. Else please tick an answer if it helped you solve your problem to keep the board clean and tidy, if nothing helped you, then we really appreciate FEEDBAC$$anonymous$$.

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by clunk47 · Sep 29, 2013 at 03:31 AM

Well you didn't tell us what language you use, so I'll give an example in C# and another in JS(UnityScript). In this example I have a public GameObject where you will need to drag a prefab from your assets folder into the slot on this component once it's attached to a gameObject in your scene. This simply creates two clones of your prefab and instantiates them in the same position. No need for a for() statement.

//C-Sharp Example.cs

 using System.Collections;
 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     public GameObject prefab;
     
     void Awake()
     {
         GameObject go1 = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
         GameObject go2 = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
     }
 }
 



//JavaScript(UnityScript) example

 #pragma strict
 
 var prefab : GameObject;
     
 function Awake()
 {
     var go1 = Instantiate(prefab, Vector3.zero, Quaternion.identity);
     var go2 = Instantiate(prefab, Vector3.zero, Quaternion.identity);
 }
 
 
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 clunk47 · Sep 29, 2013 at 03:37 AM 1
Share

If you have more than two to instantiate, it's easier to use an array and set the size of the array, then you would use a foreach() statement like so:

 using System.Collections;
 using UnityEngine;
 
 public class Example : $$anonymous$$onoBehaviour
 {
     public GameObject prefab;
     public GameObject[] gos;
     
     void Awake()
     {
         gos = new GameObject[10];
         for(int i = 0; i < gos.Length; i++)
         {
             GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
             gos[i] = clone;
         }
     }
 }
avatar image ginryu · Dec 02, 2013 at 02:09 AM 0
Share

hmm,im not doing the exact same thing... but it would seem ill have to star messing with for now.. ive only worked with if. im trying to access a single script which is attached to every game object in the array also can i do function calls on functions attached to game objects, or even script arrays(an array containing static typed scripts) stored inside arrays. if you can answer this, i will give the person who answers my global control script when its done, it will take a while to complete. but i hope that its contents will be helpful in organizing your own advanced control scripts. email me telling me that this has been answered and i will check it out. whether the answer is helpful OR not, i will still honor this deal. nd if it seems like i forgot, please re$$anonymous$$d me. i forget things easily when program$$anonymous$$g. kristopher.j.logan@gmail.com and forgive the typos i dont have a keyboard and am using my smartphone as an input device

avatar image ginryu · Dec 02, 2013 at 02:15 AM 0
Share

i need the answer in javascript please, or even both. doesnt matter

avatar image clunk47 · Dec 02, 2013 at 03:16 AM 1
Share

I realized I was creating an array and creating clones, but wasn't assigning the array indexes to the gameObjects instantiated. Edited C# example above, and here's the JS equivalent.

 #pragma strict 
 
 var prefab : GameObject;
 var gos : GameObject[];
 
 function Awake()
 {
     gos = new GameObject[10];
     for(var i : int = 0; i < gos.Length; i++)
     {
         var clone = Instantiate(prefab, Vector3.zero, Quaternion.identity);
         gos[i] = clone;
     }
 }

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Make a simple tree 1 Answer

How can I make a game object follow an instantiated game object? 1 Answer

Instantiated GameObject collision without script repetition? 1 Answer

iOS Instantiate transform as child -- positioning bug 2 Answers

How to change position of child component without affecting the position of the parent 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