Hi everyone
I just started making a simple quiz game and have been setting up a system to create buttons based on an array of an “Answer” class I’ve made.
Unfortunately, for some reason I can’t edit the variables of the Answer class in the inspector and I have no idea why. I’m sure I’ve missed something silly but below is the code I’ve done so far:
Answer Class:
using UnityEngine;
using System;
[Serializable]
public class Answer : MonoBehaviour
{
public string answerText;
public bool isCorrect;
}
The game’s manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuizController : MonoBehaviour
{
public GameObject canvas;
public GameObject button;
public Answer[] answerArray;
private void Start()
{
foreach(Answer a in answerArray)
{
CreateButton(a);
}
}
public void CreateButton(Answer answer)
{
GameObject newButton = Instantiate(button) as GameObject;
newButton.transform.SetParent(canvas.transform, false);
newButton.GetComponentInChildren<Text>().text = answer.answerText;
}
}
What is looks like in the inspector:
Thanks in advance for your help