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