Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 chusta · Dec 21, 2016 at 07:36 AM · onclicklistener

onClick listener not being added properly via script, but no errors

Hey everyone! I'm having a problem with the onClick.AddListener() function. It is not returning any errors, but it is not adding my listener properly. Here is the code i'm using:

 void SetupReloadButton()
 {
     reloadButton = GetButton("ReloadButton");
     reloadButton.onClick.AddListener(DoSomething);
 }

 public Button GetButton(string name)
 {
     GameObject go = GameObject.Find(name);
     return go.GetComponent<Button>();
 }

 void DoSomething()
 {
     Debug.Log("Did something.");
 }

I have used this same method of adding onClick listeners in other parts of my app and it works fine. There are a few things that distinguish this part of the app from the others, that might be helpful information as to why this isn't working:

  1. I instantiate tiles dynamically, only when the scene is loaded. The scene already contains a canvas with the button I care about before instantiating these tiles, but objects are added to the scene only once the scene is loaded.

  2. I wait for the scene to be loaded before I instantiate those objects. To do this I have a class that checks if the scene was loaded or not. If it is loaded, it creates the tiles. Here is that code:

      void Awake()
     {
         dynamicBoardManager = Object.FindObjectOfType<DynamicBoardManager>();
         gameManager = Object.FindObjectOfType<GameManager>();
    
         SceneManager.sceneLoaded += OnSceneLoaded;
     }
    
     void OnDestroy()
     {
         SceneManager.sceneLoaded -= OnSceneLoaded;
     }
    
     void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
     {
         dynamicBoardManager.CreateBoard(gameManager.currentWorld, gameManager.currentLevel);
     }
    
    
    

I recently overhauled how i'm building my scene (used to be static, is now dynamic) and when it was a static scene, it worked perfectly fine. So i'm scratching my head as to why this isn't working when it's dynamic, or at least why i'm not getting any errors!

Your help is much appreciated :)

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 chusta · Dec 21, 2016 at 05:51 AM 0
Share

I already found out what my problem is.... And i'll leave it here in case anyone else has a similar one, since I couldn't find a problem like $$anonymous$$e while searching.

When copying over the stuff from one scene to the next I forgot to copy over the EventSystem! So by adding an EventSystem to the scene, everything worked like I expected it to.

It was such a dumb mistake, but I still almost expect some kind of error or warning letting me know my scene doesn't contain an EventSystem if I try to add a listener to a button.

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My onclick action listeners I attach to my buttons as I instantiate them only work once 1 Answer

How do I attach Right Click to an EventSystem listener? 1 Answer

Button onClick is called when added to the Listener 2 Answers

onclick.addlistener only works once 0 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