Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ButtonCreator : MonoBehaviour {
  7. public GameObject buttonPrefab;
  8. public GameObject panelToAttachButtonsTo;
  9. // Use this for initialization
  10. void Start () {
  11. GameObject button = (GameObject)Instantiate(buttonPrefab);
  12. button.transform.SetParent(panelToAttachButtonsTo.transform);//Setting button parent
  13. button.GetComponent<Button>().onClick.AddListener(OnClick);//Setting what button does when clicked
  14. //Next line assumes button has child with text as first gameobject like button created from GameObject->UI->Button
  15. button.transform.GetChild(0).GetComponent<Text>().text = "This is button text";//Changing text
  16. }
  17.  
  18. // Update is called once per frame
  19. void Update () {
  20.  
  21. }
  22. void OnClick() {
  23. Debug.Log ("clicked!");
  24. }
  25. }
Add Comment
Please, Sign In to add comment