• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

138 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 avatar image

Related Questions

Creating working UI Button from Script? 1 Answer

AddListener function not working? 5 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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges