Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
Button.onClick.AddListener() not working as intended

I've been struggling with this for the last couple of hours. I can access and edit the properties of the button, but the onClick.AddListener() function does not work. Here is my code:

GameObject gameObject;
        gameObject = Resources.Load("Prefabs/MenuOption") as GameObject;

        gameObject.GetComponent<Image>().sprite = Resources.Load<Sprite>(option.imgPath);

        Button button = gameObject.GetComponent<Button>();

        // Testing to see if I can change button properties (works)
        ColorBlock colorss = button.colors;
        colorss.highlightedColor = Color.magenta;
        button.colors = colorss;
        
        // This does not work
        button.onClick.AddListener(() => Test(button));
        button.onClick.AddListener(() => { Debug.Log("click"); });

        Instantiate(gameObject, pos, new Quaternion(), GameObject.Find("Canvas").transform);

And here is an instance of the prefab: image

If anyone knows what I'm doing wrong, I would be very grateful.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Subscribing to onClick event on the instantiated game object (not on the game object you use to instantiate another object) should solve your problem.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda