- Home /
How to instantiate everything inside an Array?
Hi, I have an array of gameobjects. Onstart I would like all of these objects in the array to be instantiated. Although this script is attached to several other items so the amount in the array is not specific. One will have 3 items to instantiate, the other will have 5 or maybe 6. The idea is to be able to drag prefabs into the inspector to set them to instantiate on load. Here is what I have so far, can anyone tell me what I am doing wrong?
using UnityEngine;
using System.Collections;
public class MyDrops : MonoBehaviour
{
public GameObject[] Items;
//Using on start to test.
void Start ()
{
DropItems();
}
//First Attempt to instantiate all the items in the Items[] array.
void DropItems()
{
Instantiate(Items[Items.Length] ,transform.position, Quaternion.identity);
}
//Second Attempt. (Error, value expected in "Items[ here ]".)
//An Object reference is required for the non-static field, method, or property.
public static void Drop(GameObject[] itemArr)
{
foreach (GameObject i in itemArr)
{
Instantiate(Items[], transform.position, Quaternion.identity);
}
}
}
Answer by roojerry · Jan 28, 2014 at 07:24 PM
foreach (GameObject i in Items)
{
Instantiate(i, transform.position, Quaternion.identity);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate 3 guiTextures at the same 1 Answer
How to use many particle systems, just varying in size and emitter dimensions 0 Answers
Instantiating A Grass Prefab Multiple Times 1 Answer
instantiate object only once in current position and than again in another 0 Answers
instantiating multiple prefabs within an area (nearly done) 1 Answer