- Home /
GetComponent().onClick.AddListener not working
Hello everyone, I have a few dynamically generated buttons in my scene and I want to add a listener to all of them which would load a new scene (buttonClicked() method in the code). However, the listener doesn't seem to get added as the buttons don't react to being clicked but the new scene does load if I press "A" (Update() method in code). I've tried googling but none of the solutions I found seem to help. What could possibly be the problem? Thanks in advance!
Relevant code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelSelectionScript : MonoBehaviour
{
public TextAsset jsonFile;
public static int CurrentLevel = 0;
// Start is called before the first frame update
void Start()
{
init_selection();
}
// Update is called once per frame
void Update()
{
//Ends the game (closes application) if ESC is pressed
if (Input.GetKey(KeyCode.Escape))
Application.Quit();
if (Input.GetKey(KeyCode.A))
SceneManager.LoadScene("Level1", LoadSceneMode.Single);
}
void init_selection()
{
PointsData pointsData = JsonUtility.FromJson<PointsData>(jsonFile.text);
for(int i = 0; i < pointsData.levels.Length; i++)
{
GameObject btn = new GameObject();
btn.transform.SetParent(this.gameObject.transform.parent.transform);
//Adding and editing Canvas and RectTransform components
btn.AddComponent<Button>();
btn.GetComponent<Button>().name = i.ToString();
btn.GetComponent<Button>().onClick.AddListener(()=>buttonClicked());
//Adding and editing a Text component
}
}
void buttonClicked()
{
int chosenLevel = int.Parse(EventSystem.current.currentSelectedGameObject.name);
CurrentLevel = chosenLevel;
SceneManager.LoadScene("Level1", LoadSceneMode.Single);
}
}
Full code in case anyone would like to see it as well :P
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelSelectionScript : MonoBehaviour
{
public TextAsset jsonFile;
public static int CurrentLevel = 0;
// Start is called before the first frame update
void Start()
{
init_selection();
}
// Update is called once per frame
void Update()
{
//Ends the game (closes application) if ESC is pressed
if (Input.GetKey(KeyCode.Escape))
Application.Quit();
if (Input.GetKey(KeyCode.A))
SceneManager.LoadScene("Level1", LoadSceneMode.Single);
}
void init_selection()
{
PointsData pointsData = JsonUtility.FromJson<PointsData>(jsonFile.text);
for(int i = 0; i < pointsData.levels.Length; i++)
{
GameObject btn = new GameObject();
btn.transform.SetParent(this.gameObject.transform.parent.transform);
Vector3 temp = new Vector3(Screen.width/ 2, (Screen.height / pointsData.levels.Length) * (pointsData.levels.Length - (i + 1)));
btn.transform.localPosition += temp;
btn.AddComponent<RectTransform>();
btn.GetComponent<RectTransform>().sizeDelta = new Vector2(100, 50);
btn.AddComponent<Canvas>();
btn.GetComponent<Canvas>().worldCamera = Camera.main;
btn.GetComponent<Canvas>().overrideSorting = true;
btn.GetComponent<Canvas>().sortingOrder = 1;
btn.AddComponent<Button>();
btn.GetComponent<Button>().name = i.ToString();
btn.GetComponent<Button>().onClick.AddListener(()=>buttonClicked());
btn.AddComponent<Text>();
btn.GetComponent<Text>().text = "Level " + (i + 1).ToString();
btn.GetComponent<Text>().color = Color.black;
btn.GetComponent<Text>().fontSize = 30;
btn.GetComponent<Text>().horizontalOverflow = HorizontalWrapMode.Overflow;
btn.GetComponent<Text>().verticalOverflow = VerticalWrapMode.Overflow;
Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
btn.GetComponent<Text>().font = ArialFont;
btn.GetComponent<Text>().raycastTarget = false;
}
}
void buttonClicked()
{
int chosenLevel = int.Parse(EventSystem.current.currentSelectedGameObject.name);
CurrentLevel = chosenLevel;
SceneManager.LoadScene("Level1", LoadSceneMode.Single);
}
}
Comment