Get a reference to a button from script in Unity

25,580

Looking at your code, there is another problem:

shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;

You cannot cast Component(Button) to GameObject like that. You have to use GetComponent to get the Button component from the GameObject.

I cannot drag and drop the button in the inspector and when I find it by tag I cannot cast it from GameObject to Button

That's because the the GameObject you are dragging to the shootButton slot is not a Button or does not have the Button component attached to it. It must have the Button component for you to be able to drag it to the Button(shootButton ) slot.

You have to create a Button then drag that Button to the shootButton slot.

You can do that by first removing

shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;
shootButton.onClick.AddListener ((UnityEngine.Events.UnityAction)this.OnShootButtonClick);

then drag the Button to the shootButton slot:

enter image description here

OR

get the reference from script:

If you want to do this from script, replace

shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;
shootButton.onClick.AddListener ((UnityEngine.Events.UnityAction)this.OnShootButtonClick);

with

shootButton = GameObject.FindGameObjectWithTag("ShootButton").GetComponent<Button>();
shootButton.onClick.AddListener(() => OnShootButtonClick());
Share:
25,580
Rango
Author by

Rango

Updated on September 22, 2020

Comments

  • Rango
    Rango over 3 years

    The script is attached to a prefab that is not in a scene. The button has its tag.

    I tried drag and dropping the button in the inspector, but the engine won't let me. I tried finding it by tag, but I get an exception "Cannot implicitly convert type UnityEngine.GameObject to UnityEngine.UI.Button " and when I cast I get an exception that I cannot convert these types via built-in conversion. Some help? How do I get a reference to a button? Here is the code :

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class TankShooting : MonoBehaviour {
    
        private Transform ShootingCameraTransform;
        private PlayerTankMovement playerTankMovement;
        public Button shootButton;
    
    
        // Use this for initialization
        void Start () {
    
            shootButton = GameObject.FindGameObjectWithTag ("ShootButton") as Button;
            shootButton.onClick.AddListener ((UnityEngine.Events.UnityAction)this.OnShootButtonClick);
    
            playerTankMovement = GetComponent<PlayerTankMovement> ();
            Transform t = transform;
            foreach (Transform tr in t)
            {
                if (tr.tag == "ShootingCamera") 
                {
                    ShootingCameraTransform = tr.transform;
                }
            }
        }
    
        // Update is called once per frame
        void Update () {
    
        }
    
        public void OnShootButtonClick()
        {
            Debug.Log ("Success");
    
        }
    }