I have a script main.cs
which creates a prefabricated button based on this SO post. I thought btnController.cs
was providing the methods for the Button
created in main.cs
? Why would it have a casting issue?
Error
Assets/scripts/main.cs(21,51): error CS0030: Cannot convert type `UnityEngine.UI.Button' to `btnController'
main.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class main : MonoBehaviour {
public Button myButtonPrefab;
// Use this for initialization
void Start () {
createButton ("minion");
}
// Update is called once per frame
void Update () {
}
//this is giving a button prefab all the methods from the class btnController
void createButton(string name){
btnController b = ((btnController)Instantiate(myButtonPrefab));
b.Setup(name,0);
}
}
btnController.cs
using UnityEngine;
using System.Collections;
public class btnController : MonoBehaviour {
string name="";
int value=0;
public void Setup(string n, int v){
name = n;
value = v;
}
void OnClick(){
Debug.Log("btn clicked");
}
}