Instantiate GameObject From Array c#

Hi All,

I have any problem when i try to instantiate a game object using array…
This is my script :

using UnityEngine;
using System.Collections;

public class GUI_Array : MonoBehaviour {

    // masukkan objek tanda taktis
    public GameObject ANG;
    public GameObject ARHANUD;
    public GameObject ARMED;
    public GameObject HUB;
    public GameObject INF;
    public GameObject KAV;
    public GameObject KES;
    public GameObject PAL;
    public GameObject PNB;
    public GameObject POM;
    public GameObject ZI;
    //

    public Transform Kotak; // memasukkan cube sebagai posisi awal yang di load

    public Rect windowRect = new Rect(20, 20, 120, 50); //membuat layar windows GUI
    string[] buttonName =new string[11] {"ANG","ARHANUD","ARMED","HUB","INF","KAV","KES","PAL","PNB","POM","ZI"}; // Array dengan type data string

    void OnGUI ()
    {
        windowRect = GUILayout.Window(0, windowRect, textButton, "Membuat Skenario", GUILayout.Width(200));

    }

    void textButton(int winID){
        for(int i = 0; i<11; i++) //perulangan untuk menambah tombol sebanyak index pada array
        {
            if(GUILayout.Button(buttonName[i])){
                print("I pressed " + buttonName[i]);
                //Instantiate(ANG, Kotak.position, Kotak.rotation);   // --> This is just one of game object can be instantiate with every button
            }
        }

    }
}

How can to solved it…Thank’s before… :slight_smile:

Its easier to add the items in one Array, call the item from the for loop like so

This was you just set your items array to 11, add your GO’s and then when you hit a button it should instantiate it, Untested but it should be right.

using UnityEngine;
using System.Collections;
public class GUI_Array : MonoBehaviour {
    // masukkan objek tanda taktis
    public GameObject[] Items; //Add the "ANG","ARHANUD","ARMED","HUB","INF","KAV","KES","PAL","PNB","POM","ZI" objects in the editor
    //
    public Transform Kotak; // memasukkan cube sebagai posisi awal yang di load
    public Rect windowRect = new Rect(20, 20, 120, 50); //membuat layar windows GUI
    string[] buttonName =new string[11] {"ANG","ARHANUD","ARMED","HUB","INF","KAV","KES","PAL","PNB","POM","ZI"}; // Array dengan type data string
    void OnGUI ()
    {
        windowRect = GUILayout.Window(0, windowRect, textButton, "Membuat Skenario", GUILayout.Width(200));
    }
    void textButton(int winID){
        for(int i = 0; i<11; i++) //perulangan untuk menambah tombol sebanyak index pada array
        {
            if(GUILayout.Button(buttonName[i])){
                print("I pressed " + buttonName[i]);
               
                Instantiate(Items[i], Kotak.position, Kotak.rotation);
            }
        }
    }
}