Question by
Voodin · May 02, 2019 at 06:57 PM ·
uiprefabsruntime-generation
How do I instantiate UI prefabs
I want to make a game with a dialog tree, so I want to be able to instantiate option boxes, depending on the amount of options available with each line of dialog. I've made my individual dialog nodes as scriptable objects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
[CreateAssetMenu(fileName="speak",menuName="Dialog")]
public class Dialog : ScriptableObject
{
public string Text;
public string[] Choices;
private GameObject preFab = GameObject.Find("DialogOption");
public void instantiateChoice()
{
for(int i=0;i<=Choices.Length;i++)
{
//instantation
GameObject option = Instantiate(preFab,new Vector2(0,0),Quaternion.identity);
}
}
}
I haven't finished that yet so I'm using my player controller to test the instantiation.
private GameObject preFab;
...
void Start () {
preFab = GameObject.Find("DialogOption");
}
...
if(Input.GetKeyDown("space"))
{
GameObject option = Instantiate(preFab,new Vector2(0,0),Quaternion.identity);
}
I excluded the rest of the code in that script. I press space, and nothing happens, another DialogOption doesn't appear on the screen, nor does it appear in the editor heirarchy.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612200615im_/https://answers.unity.com/themes/thub/images/avi.jpg)