Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a reference to a button from script in Unity

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");

    }
}
like image 827
Rango Avatar asked Sep 22 '16 07:09

Rango


People also ask

How do you get a script reference in unity?

Both the script and the audio source are attached to the same game object, which in this case is the player, and connecting the two is as easy as dragging the audio source component to the player audio source field on the script component. Click and drag the component to the empty field to create the reference.

How do I use buttons in unity script?

To insert a button, right click in the Scene Hierarchy and go to Create → UI → Button. If you do not have an existing Canvas and an EventSystem, Unity will automatically create one for you, and place the button inside the Canvas as well.


1 Answers

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());
like image 165
Programmer Avatar answered Sep 18 '22 15:09

Programmer