Search code examples
user-interfaceeventsaudiounity-game-enginebuttonclick

How to access the OnClick event via Script


I am trying to do a SongManager object which will be responsible for changing songs. I created a SongManager and a Song script. On runtime SongManager creates as many song buttons as i want each with different variables. Everything seems to work fine except that i cant get to OnClick event to change the songs.I tried many things but i guess its all outdated. Stuff like:

public Button.ButtonClickedEvent OnClickEvent;

or

go.GetComponent<Button>().onClick.AddListener();

Appreciate any help, thank u guys.

enter image description here

enter image description here

enter image description here

enter image description here


Solution

  • You can use Events for comminucation with your other classes. Here is the code :

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class SongButton : MonoBehaviour {
    
        public delegate void SongButtonEvent(int index);
        public static event SongButtonEvent OnSongButtonClick;
        void ClickSongButton(){
           if (OnSongButtonClick != null)
                OnSongButtonClick (index);
        }
    
        public Button button;
        public Text songName;
        public int unlocked;
        public AudioClip clip;
        public int index;
    
        void Start () {
            button.onClick.AddListener (ClickSongButton);
        }
    
        public void Initialize(int index,Song song){
            this.index = index;
            songName.text = song.songName;
            unlocked = song.unlocked;
            clip = song.clip;
            button.interactable = song.isInteractable;
        }
    }
    

    First of all, i create a Initialize method. It uses Song object variables for initialization and takes an index as button id. After i create an event for notify listeners with index.

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using System.Collections.Generic;
    
    [System.Serializable]
    public class Song
    {
        public string songName;
        public int unlocked;
        public bool isInteractable;
        public AudioClip clip;
    }
    
    public class SongManager : MonoBehaviour
    {
    
        public SongButton button;
        public Transform panel;
        public List<Song> songList;
    
        void OnEnable(){
            SongButton.OnSongButtonClick += SongButton_OnSongButtonClick;
        }
    
        void OnDisable(){
            SongButton.OnSongButtonClick -= SongButton_OnSongButtonClick;
        }
    
        void SongButton_OnSongButtonClick (int index)
        {
             Debug.Log ("index : " + index + " - song name : " + songList [index].songName);
        }
    
        void Start ()
        {  
           FillList ();    
        }
    
        void FillList ()
        {  
            for (int i = 0; i < songList.Count; i++) {
                SongButton songButton = Instantiate (button) as SongButton;  
                songButton.Initialize (i, songList [i]);
                songButton.transform.SetParent (panel, false);
            } 
        }
    }
    

    When SongManager became enable, it starts to listen to OnSongButtonClick events. This way, you can know which button clicked.

    I hope it helps.