Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Referencing a prefab button

Discussion in 'Scripting' started by eelir_g, Mar 11, 2021.

  1. eelir_g

    eelir_g

    Joined:
    Aug 23, 2019
    Posts:
    6
    I am passing a prefab of a button in my script, but I cant reference the button. I am getting the

    I tried to use goButton.GetComponent<Button>() but it gives me the same error.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.  
    4.     public int numberOfLetters;
    5.     public GameObject buttonPrefab;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         GameObject.Find("GameCanvas").GetComponent<UnityEngine.Canvas>();
    10.         GameObject panel = GameObject.Find("Panel").gameObject;
    11.  
    12.         GameObject goButton = Instantiate(buttonPrefab, panel.transform) as GameObject;//, new Vector3 (0,0,0), Quaternion.identity) as GameObject;
    13.  
    14.         TMPro.TextMeshProUGUI tmptxt = goButton.GetComponentInChildren<TMPro.TextMeshProUGUI>();
    15.  
    16.      
    17.         Transform t = panel.transform.Find("btnLetters(Clone)").GetComponentInChildren<Transform>();
    18.    
    19.         tmptxt.text = "B";
    20.  
    21.  
    22.         goButton.transform.localPosition = new Vector3(-50, -200, 0);
    23.  
    24.         t.GetComponent<Button>().clicked += () => PressButton(tmptxt.text);
    25.      
    26.  
    27.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Sounds like you have your own class called Button.

    Fix it by either renaming your class, or putting something like this at the top of the file:
    using Button = UnityEngine.UI.Button;
     
    Putcho likes this.
  3. eelir_g

    eelir_g

    Joined:
    Aug 23, 2019
    Posts:
    6
    Thanks for the reply. I have only one script and do not have any Button class defined. Adding the line you suggested did not fix it, and now i have more errors besides the first one.
     
  4. eelir_g

    eelir_g

    Joined:
    Aug 23, 2019
    Posts:
    6
    Huh, I changed the
    to

    So if i never created a Button class, how is it that it is there?
     
  5. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    isn't PraetorBlue want you to do this?
    Code (CSharp):
    1. t.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(delegate { PressButton(str);});
     
  6. eelir_g

    eelir_g

    Joined:
    Aug 23, 2019
    Posts:
    6
    Yeah Putcho, I figured it out as well, but I am confused as to why is there two Button classes in my project? I am using the TMP button, could that be causing issues?
     
  7. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    TMP using unity's standard Button with TMP text as a child.

    it can be some assets from assets store or 3. party plugins, you didn't show using at the top of your class so I can't tell, but there are 4 namespaces from UnityEngine that has it own Button Class

    using static UnityEngine.InputSystem.HID.HID;
    using static UnityEngine.Rendering.DebugUI;
    using UnityEngine.UI;
    using UnityEngine.UIElements; <------ I think your class using this one by default
     
    eelir_g, PraetorBlue and Kurt-Dekker like this.