Skip to content

Instantly share code, notes, and snippets.

@tsonglew
Created July 19, 2018 23:17
Show Gist options
  • Save tsonglew/40029d090c704538df6b18717200165f to your computer and use it in GitHub Desktop.
Save tsonglew/40029d090c704538df6b18717200165f to your computer and use it in GitHub Desktop.
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!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment