5
votes

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

    }
}
1
Hi, since you used this solution, why not accept it as answer? - Programmer
I''m not sure that it works. I have a problem referencing a button. If it works I will accept it. It's the checkmark thing on the left right? Can you help with the referencing the button? - Rango
Yes, the checkmark thing not the upvote or downvote image. So, if you ask a question and the answer you got does not work, do not create a new one with the non working answer. Instead, leave a comment under the answer. I am sure those people are willing to help you more answer your question. If you ignore them and then use their code in a new question, it means that the code they made solved your problem and you are now having a new different problem. - Programmer
Thanks on the advice. I will used it in the future - Rango
Ok, so what's your problem? - Programmer

1 Answers

8
votes

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