title: Adding button with Text dynamically to UI at runtime in Unity
body: <p>I am working with <code>C#</code> an <code>Unity 5.6.1f1</code>, I done a mechanism that on UI button click should add an additional button with text to Panel that is already on UI, and it does work... since I am able to see Object appear there </p>
  
  <p><a href="https://i.stack.imgur.com/RTwUD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RTwUD.png" alt="enter image description here"></a></p>
  
  <pre><code>public void MakeButton(string direction)
  {
  
      GameObject button = new GameObject();
      button.AddComponent&lt;RectTransform&gt;();
      button.AddComponent&lt;Button&gt;();
  
      var panel = GameObject.Find("CommandPanel");
      button.transform.position = panel.transform.position;
      button.GetComponent&lt;RectTransform&gt;().SetParent(panel.transform);
      button.GetComponent&lt;RectTransform&gt;().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left,0,10);
      button.SetActive(true);
      button.layer = 5;
  
      //button.GetComponentsInChildren&lt;Text&gt;()[0].text = "New Super Cool Button Text";
  
  }
  </code></pre>
  
  <p>problem is it isn't visible and reason for that I believe that there is no text there... now how do I know that...</p>
  
  <p>I tried running the following <code>button.GetComponentsInChildren&lt;Text&gt;()[0].text = "New Super Cool Button Text";</code></p>
  
  <p>and it gave me </p>
  
  <blockquote>
    <p>IndexOutOfRangeException: Array index is out of range.
    Hero.MakeButton (System.String direction) (at Assets/Scripts/Hero.cs:37)
    Hero.Down () (at Assets/Scripts/Hero.cs:14)
    UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
    UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
    UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update()</p>
  </blockquote>
  
  <p>Also as you can see in the screenshot there is no triangle Under <code>New Game Object</code> like it is under <code>Run</code> which is also a button...</p>
  
  <p><strong>How do I add from the script Button that has Text on it to panel in Unity?</strong> </p>