- Home /
Question by
MrAwesome343 · Jun 02, 2017 at 02:45 PM ·
instantiatebuttoncanvasprefabs
Creating Buttons with a C# Script
I'm trying to look at an array of text and use that to determine how many buttons I need and what the text on the buttons say, but, so far, I can't even create the buttons. They also need to be aligned on a Horizontal Layout.
void UpdateDialogue()
{
if (characterName.Equals("Questions"))
{
CreateNewQuestionObject();
}
}
void CreateNewQuestionObject()
{
question = (GameObject)Instantiate(Resources.Load("QuestionTemplate"));
question.transform.SetParent(questionPanel.transform);
}
Comment
Best Answer
Answer by Hellium · Jun 02, 2017 at 05:16 PM
Check the manual : https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html
// Drag & Drop the prefab of your button
public GameObject QuestionTemplate;
// Drag & Drop the gameobject with the Horizontal layout
public RectTransform questionsPanel ;
void UpdateDialogue()
{
if (characterName.Equals("Questions"))
{
CreateNewQuestionObject();
}
}
void CreateNewQuestionObject()
{
RectTransform questionTransform = ((GameObject)Instantiate( QuestionTemplate )).GetComponent<RectTransform>();
questionTransform.SetParent(questionsPanel );
}