• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DaPizzaKing · Mar 03, 2018 at 01:48 AM · uibuttonbuttonsonclicklistener

button.onClick.AddListener(method); NOT Working

So I'm creating a very basic menu with 3 buttons. I had the buttons working earlier today to the point that they would do what they should have. Unfortunately, because of some tweaking I did, the onClick function doesn't seem to be working for me. I would love some insight, thank you!

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

 public class PauseUI : MonoBehaviour {
     private Component[] buttons, screens;
     private Canvas pauseMenu;
     private Button resumeGame, exitGame;
     private Camera characterCam, pauseCam;
     void Start () 
     {
         characterCam = GameObject.Find ("MainCamera").GetComponent<Camera>();
         pauseCam = this.GetComponent<Camera> ();
         buttons = GetComponentsInChildren<Button> ();
         screens = GetComponentsInChildren<Canvas> ();
         foreach (Button button in buttons) {
             if (button.name == "ResumeGame") {
                 resumeGame = button;
             }
             if (button.name == "ExitGame") {
                 exitGame = button;
             }
         }
 
         foreach (Canvas canvas in screens) {
             if (canvas.name == "PauseMenu") {
                 pauseMenu = canvas;
             }
         }
         characterCam.enabled = true;
         pauseCam.enabled = false;
         pauseMenu.enabled = false;
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyDown (KeyCode.Escape)) {
             pauseMenu.enabled = true;
             characterCam.enabled = false;
             pauseCam.enabled = true;
         }
         resumeGame.onClick.AddListener (ResumeGame);
     }
 
     void ResumeGame(){
         Debug.Log ("ResumeGame has been clicked!");
         pauseMenu.enabled = false;
         characterCam.enabled = true;
         pauseCam.enabled = false;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Mar 03, 2018 at 09:18 AM 0
Share

You are adding a new listener to the button EVERY FRA$$anonymous$$E

Have you checked the answer I gave here? https://answers.unity.com/questions/1402544/so-my-buttons-stop-working.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by upasnavig90 · Mar 03, 2018 at 09:21 AM

try this: resumeGame.onClick.AddListener (()=>ResumeGame);

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Mar 03, 2018 at 09:41 AM 1
Share

This won't compile.

If you meant

 resumeGame.onClick.AddListener (()=>ResumeGame());

This will be worse than resumeGame.onClick.AddListener (ResumeGame); because you create a useless lambda function.

avatar image upasnavig90 Hellium · Mar 03, 2018 at 09:51 AM 1
Share

Actually you are right, i didn't check the code, @DaPizza$$anonymous$$ing is adding listener in every frame in update, which is wrong. @DaPizza$$anonymous$$ing , adlistener should be used once and it will work throughout.

avatar image DaPizzaKing upasnavig90 · Mar 03, 2018 at 07:20 PM 0
Share

I got it to work, thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating working UI Button from Script? 1 Answer

AddListener function not working? 6 Answers

How to ADD one Buttons.onClick events to another Buttons.onClick? 1 Answer

Runtime UI button creation: can't make an "OnClick" where I call a function from another script while sending a parameter. 2 Answers

Weird behaviour when trying to dynamically create buttons 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges