Search Unity

How to Create UI Button Dynamically

Discussion in 'Scripting' started by Alexander21, Mar 22, 2016.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hi all

    I am new bie unity. In my project i have to create 5 UI Button Dynamically using array list.and it display which

    button is pressed. Could any one help me..

    Thanks in Advance
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    1. Make a nice button and save it as prefab.
    2. Instantiate it or Pool it.
    3. Add onClick events.

    Confusing?
    A simple example for you.:)

    Code (CSharp):
    1.  GameObject buttonPrefab;
    2.  
    3.     void MyAwesomeCreator()
    4.     {
    5.         GameObject go = Instantiate(buttonPrefab);
    6.         var button = GetComponent<UnityEngine.UI.Button>();
    7.         button.onClick.AddListener(() => FooOnClick());
    8.     }
    9.     void FooOnClick()
    10.     {
    11.         Debug.Log("Ta-Da!");
    12.     }
     
  4. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Thanks eses and zhuchun for you help. When i run the above code first time it works . After that i run in another project it says

    Type `Button' does not contain a definition for `onClick' and no extension method `onClick' of type `Button' could be found (are you missing a using directive or an assembly reference?)

    I dont know how to solve this.. Could any one help.. Why it creates error........
     
  5. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hi i have tested with the below code...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.    
    7.     public GameObject prefabButton;
    8.     public RectTransform ParentPanel;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.         for(int i = 0; i < 5; i++)
    14.         {
    15.             GameObject goButton = (GameObject)Instantiate(prefabButton);
    16.             goButton.transform.SetParent(ParentPanel, false);
    17.             goButton.transform.localScale = new Vector3(1, 1, 1);
    18.            
    19.             Button tempButton = goButton.GetComponent<Button>();
    20.             int tempInt = i;
    21.            
    22.             tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
    23.         }
    24.        
    25.        
    26.     }
    27.    
    28.     void ButtonClicked(int buttonNo)
    29.     {
    30.         Debug.Log ("Button clicked = " + buttonNo);
    31.     }
    32.    
    33. }
    First Time it runs good. After that when i copied to another project. It shows the error....

    NullReferenceException: Object reference not set to an instance of an object

    Could any one help me... How to Solve the Problem.........
     
    lorinmanuo and IT-BaseX like this.
  6. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    I have run many button cs coding files from forum . It shows the same error......could any one help me....

    NullReferenceException: Object reference not set to an instance of an object
     
  7. Jason_ioosh

    Jason_ioosh

    Joined:
    Mar 21, 2016
    Posts:
    16
    Hi @Alexander21,

    Could you perhaps give more information? Which line of code is the Error showing up at?

    Could you perhaps also take a screenshot and show the actual Error?

    Regards,
    Jason
     
  8. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Thanks Jason for your help. Actually While run time it creates error. It creates single button after it shows the error.

    NullReferenceException: Object reference not set to an instance of an object

    while i click that error. It Points to this line..

    tempButton.onClick.AddListener(()=> ButtonClicked(tempInt));

    could any one help me......
     
  9. Jason_ioosh

    Jason_ioosh

    Joined:
    Mar 21, 2016
    Posts:
    16
    Hi @Alexander21,

    I ran your code in my game, the first time I ran it, it was fine.

    And I managed to replicate your Error by removing the Button component from your prefab.

    I've attached 2 images to help you in your understanding.

    To get the results in 7.png, go to your prefabButton and addComponent -> Button.

    Edit: I do not know if this is your exact problem, so please test and let me know the results.

    Regards,
    Jason
     

    Attached Files:

    • 7.PNG
      7.PNG
      File size:
      13.5 KB
      Views:
      1,868
    • 8.PNG
      8.PNG
      File size:
      4.6 KB
      Views:
      1,754
  10. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Thanks jason.. I have added the button component but The same 8.png error is happend while running my script...I am trying soon i will update
     
  11. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Hey, sorry for the confusing. You should have an Button attach on it, a safer way is RequireComponent, it checks your GameObject and provides suggestion. This header ensure that you won't get null when GetComponent<T>.
    Code (CSharp):
    1. [RequireComponent(typeof(UnityEngine.UI.Button))]
    2. public class MyAwsomeScriptAttachOn: MonoBehaviour
    3. {
    4. var button = GetComponent<UnityEngine.UI.Button>(); //button can't be null
    5. }
     
    iTWetrade, AjBaruah and oxcamel like this.
  12. SpandanaMatapathi

    SpandanaMatapathi

    Joined:
    Aug 10, 2020
    Posts:
    3

    You have not attached the prefab and the panel to your script components in unity...i think this is the reason
     
  13. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    Yup and the OP specifically asked about instantiating a button, not a prefab you're made prior.