Skip to main content
Time between when the question is asked and the answer is irrelevant.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

I know, it's now about one year since this question was about one year ago, but if anyone finds this thread, I have another (I think much better) answer. When I created a quest system for a simple RPG, I also needed a List or array in buttons. I found out, that a 'List' and 'foreach' is a much easier way to do this so here is my code:

public List<Quest> activeQuests = new List<Quest> ();

public GameObject ButtonParent;

public GameObject ButtonCanvas;

void Update () {
    if (Input.GetKeyDown(KeyCode.Q)) {
        if (ButtonCanvas.activeInHierarchy) {

            ButtonCanvas.SetActive(false);

        } else {
            ButtonCanvas.SetActive (true);

            foreach (Quest currentQuest in activeQuests) {
                GameObject currentButton = Instantiate (ButtonPrefab, ButtonCanvas.transform, false);
                //use currentQuest for your GameObject like Array[i] in for
            }
        }
    }
}

I know, it's now about one year since this question was about one year ago, but if anyone finds this thread, I have another (I think much better) answer. When I created a quest system for a simple RPG, I also needed a List or array in buttons. I found out, that a 'List' and 'foreach' is a much easier way to do this so here is my code:

public List<Quest> activeQuests = new List<Quest> ();

public GameObject ButtonParent;

public GameObject ButtonCanvas;

void Update () {
    if (Input.GetKeyDown(KeyCode.Q)) {
        if (ButtonCanvas.activeInHierarchy) {

            ButtonCanvas.SetActive(false);

        } else {
            ButtonCanvas.SetActive (true);

            foreach (Quest currentQuest in activeQuests) {
                GameObject currentButton = Instantiate (ButtonPrefab, ButtonCanvas.transform, false);
                //use currentQuest for your GameObject like Array[i] in for
            }
        }
    }
}

I have another (I think much better) answer. When I created a quest system for a simple RPG, I also needed a List or array in buttons. I found out, that a 'List' and 'foreach' is a much easier way to do this so here is my code:

public List<Quest> activeQuests = new List<Quest> ();

public GameObject ButtonParent;

public GameObject ButtonCanvas;

void Update () {
    if (Input.GetKeyDown(KeyCode.Q)) {
        if (ButtonCanvas.activeInHierarchy) {

            ButtonCanvas.SetActive(false);

        } else {
            ButtonCanvas.SetActive (true);

            foreach (Quest currentQuest in activeQuests) {
                GameObject currentButton = Instantiate (ButtonPrefab, ButtonCanvas.transform, false);
                //use currentQuest for your GameObject like Array[i] in for
            }
        }
    }
}
Source Link

I know, it's now about one year since this question was about one year ago, but if anyone finds this thread, I have another (I think much better) answer. When I created a quest system for a simple RPG, I also needed a List or array in buttons. I found out, that a 'List' and 'foreach' is a much easier way to do this so here is my code:

public List<Quest> activeQuests = new List<Quest> ();

public GameObject ButtonParent;

public GameObject ButtonCanvas;

void Update () {
    if (Input.GetKeyDown(KeyCode.Q)) {
        if (ButtonCanvas.activeInHierarchy) {

            ButtonCanvas.SetActive(false);

        } else {
            ButtonCanvas.SetActive (true);

            foreach (Quest currentQuest in activeQuests) {
                GameObject currentButton = Instantiate (ButtonPrefab, ButtonCanvas.transform, false);
                //use currentQuest for your GameObject like Array[i] in for
            }
        }
    }
}