- Home /
How to Instantiate a Button in a Constructor(if possible)
Hello Everyone,
This is my first question on the unity answer forums and I've been playing around with unity for a while but the new UI features are causing some problems for me.
I am working on a game where there is a unit roster list, player clicks a button and a new unit is added to the roster. The unit is a empty game object which when constructed (should) create a child button object which players can click to access the class. The problem is that Unity.UI.Button.Button() cannot be reached because of its protection level. Here is my code when a unit is created
    class BattalionUnit{
         
     GameObject battalion;
     public Button battalionButton;
     string battalionName { get; set;}
 
     public BattalionUnit(string battalionName, Vector3 setposition)
     {
         battalion = new GameObject(battalionName);
         battalion.transform.parent = GameObject.FindGameObjectWithTag ("Canvas").transform;
         battalionButton = new Button ();
         battalionButton.transform.parent = battalion.transform;
 
 
         battalion.transform.position = setposition;
     }
Answer by benjiboy12 · Dec 10, 2014 at 01:46 PM
For anyone looking for an answer, I found one, you can just make a prefab of a button object and just instantiate it.
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
UI Escape menu script not working. 2 Answers
Request feedback for C# script (instantiating UI elements depending on Player Input) 0 Answers
How to make a GUI Tabel. 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                