- Home /
Instantiation UI elements help
Hello, I have a question about using Instantiate to procedurally make game objects out of prefabs. I have the following code that was slightly modified from somewhere else
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class InstanTrial : MonoBehaviour {
public GameObject canvas;
public GameObject panel;
// Use this for initialization
void Start () {
GameObject newpanel = Instantiate(panel) as GameObject;
newpanel.transform.SetParent(canvas.transform, false);
}
// Update is called once per frame
void Update () {
}
}
And once I place my canvas and prefab, this creates a game object of the prefab in the exact location that the prefab was created in, which all makes sense.
So I have a couple questions/problems with this. 1. If this game object is really a UI panel with a text in it, and I want to the text to change based on an assigned value, how would I do this? I know about creating a variable and assigning that variable to the text object, I'm curious about how to access it. 2. maybe this is connected to the question above, but is there any way to create this without it being a generic (or empty?) game object? 3. I get why the game object is created in the same location the prefab was because it's basically using it's/the canvases transform, but If I try to change the transform to something I select, I just can't figure out how to to get it to work out.
I know I'm asking a lot out of this "single" question and if I need to I can break this down into different discussions. But I appreciate any feedback I can get.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152014im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUIText transition trouble 1 Answer
Multiple gui instantiation issues 0 Answers
Help making a damage number system 1 Answer
Loading a text document without spaces 1 Answer
Change The GuiText.text by code 1 Answer