Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!