Create Button via Script not working
I have a code that take canvas and button prefab but after I run the code. 1) The button is not inside the canvas I instantiated. 2) I don't see any button being instantiated in my game scene (but there is one in hierarchy )`public GameObject buttonPrefab; public GameObject canvasPrefab;
// Use this for initialization
void Start () {
Canvas canvas = Instantiate (canvasPrefab, Vector3.zero, Quaternion.identity) as Canvas;
Button button = Instantiate (buttonPrefab, Vector3.zero, Quaternion.identity) as Button;
button.transform.SetParent (canvas.transform,false);
}`
Thank you very much.
Answer by vittu1994 · May 28, 2016 at 02:47 PM
Try to define and instantiate the canvas and button as GameObject
you defined them as GameObject variables and you should also try to Instantiate them as GameObjects:
GameObject canvas = Instantiate(canvasPrefab, Vector3.zero, Quaternion.identity)
as GameObject
... etc.
Your answer
Follow this Question
Related Questions
Can't find StartHost() function for a button 1 Answer
Problem with creating button via script. 1 Answer
Disabling random GUI Button from array 0 Answers
Canvas not filling up the whole screen in Unity 5 0 Answers
WEIRD EVENT SYSTEM BUTTON SELECTION 0 Answers