OnClick.Addlistener not working

Hello all, I’m doing something very simple, opening a panel on a button click, like i’ve done this soo many times just this isn’t working for some reason

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharacterSelection : MonoBehaviour
{
    [Header("Buttons...")]
    public Button ButtonBrawlers;
    public Button ButtonExitBrawlers;

    [Header("Panels...")]
    public GameObject PanelBrawlers;
    public GameObject PanelLobby;

    void Update()
    {
        ButtonBrawlers.onClick.AddListener(Brawlers);
    }

    public void Brawlers()
    {
        PanelLobby.SetActive(false);
        PanelBrawlers.SetActive(true);
    }
}

Like if it’s some simple error I’m sorry. I’m just gonna stop unity for a week to be honest :face_with_spiral_eyes:

I could’ve done something as simple as

ButtonBrawlers.onClick.AddListener(() => PanelBrawlers.SetActive(true));

But since I want to close the lobby panels, I don’t think you can do multiple action with that way I’m doing above. Reason I want to close the Lobby panel is because I have a 3d model displayed there, which will be also showed on the Brawlers panel, which I don’t want

Okay looking into this even more, I can’t even click the button’s. The button should change it’s colour when I click but it isn’t???

Why are you adding listeners in Update? Did you mean to use Awake or Start?

Not sure how UnityEvents handle being assigned the same listener multiple times, but it’s possible that’s what’s causing the problem.

2 Likes

Yeah probably wanted to use Awake, I was soo tired. But the problem is that the button isn’t even been clicked? The problem is with my canvas. I think it’s because I made it Screen Space - Camera, and added a camera, the button’s aren’t being clicked

Okay so not knowing what the problem was why buttons weren’t being pressed, I just created the whole thing again and works fine. But does anyone have any clue why the button’s weren’t being pressed before?

Sorry for necro-ing, but I’d thought I’d post in case anyone else had this issue. I would bet you were missing an event system which would explain why your button inputs also weren’t being detected, and why making a completely new Canvas (which automatically also creates and event system object) fixed it.

3 Likes