Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class ButtonCreator : MonoBehaviour {
- public GameObject buttonPrefab;
- public GameObject panelToAttachButtonsTo;
- // Use this for initialization
- void Start () {
- GameObject button = (GameObject)Instantiate(buttonPrefab);
- button.transform.SetParent(panelToAttachButtonsTo.transform);//Setting button parent
- button.GetComponent<Button>().onClick.AddListener(OnClick);//Setting what button does when clicked
- //Next line assumes button has child with text as first gameobject like button created from GameObject->UI->Button
- button.transform.GetChild(0).GetComponent<Text>().text = "This is button text";//Changing text
- }
- // Update is called once per frame
- void Update () {
- }
- void OnClick() {
- Debug.Log ("clicked!");
- }
- }
Add Comment
Please, Sign In to add comment