- Home /
The question is answered, right answer was accepted
[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?
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$$.
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);
}
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;
}
}
}
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
i need the answer in javascript please, or even both. doesnt matter
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
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