Jump to content
Unity Insider Forum

Buttons dynamisch erstellt

Recommended Posts

Guten Abend,

ich habe einen Button erstellt, ihm das Skript angehängt und dann daraus ein Prefab gemacht. Eigentlich sollte nun in der Konsole etwas ausgegeben werden, wenn einer der Button geklickt wird, tut es aber leider nicht.
War mein vorgehen unvollständig?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
    public GameObject prefabButton;
    public RectTransform ParentPanel;
    // Use this for initialization
    void Start () {
        for(int i = 0; i < 5; i++)
            GameObject goButton = (GameObject)Instantiate(prefabButton);
            goButton.transform.SetParent(ParentPanel, false);
            goButton.transform.localScale = new Vector3(1, 1, 1);
            Button tempButton = goButton.GetComponent<Button>();
            int tempInt = i;
            tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
    void ButtonClicked(int buttonNo)
        Debug.Log ("Button clicked = " + buttonNo);


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...