Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Icepick · Sep 15, 2016 at 04:23 AM · uibutton trigger events

OnClick event receiving GameObject from previous Click

I'm working on a menu system using canvas UI stuff, and I'm having a strange issue with the OnClick functions for Buttons. My current setup, I have buttons with an OnClick function, OpenMenu(GameObject), with each one given a specific object that contains buttons for other parts of the menu (for example the options screen or the level select screen.) Each of these menus have a Back button which opens the Main menu object. All of this works fine, but I wanted the Escape key to also return to the Main menu. The issue I am having is that after pressing the escape key to return to the main menu (which works as expected), the next time I click a button, the OpenMenu function on the OnClick event is receiving the GameObject from the previous click instead of the object attached to the OnClick event of the actual button I clicked.

So, for example, if I click Options and then press Escape instead of clicking Back, and then click Level Select, the Options window will open again. I've confirmed in the VS2015 debugger that the OpenMenu function is actually receiving the Options object instead of the LevelSelect object I would expect in this case.

My OnClick function is as follows: public void OpenMenu( GameObject menu ) { m_activeMenu.SetActive(false); m_activeMenu = menu; m_activeMenu.SetActive(true); }

and pressing escape simple calls OpenMenu(m_mainMenu); Both m_mainMenu and m_activeMenu are GameObjects that serve as parents to the other UI elements on those menus but are otherwise empty themselves. The button OnClick events are setup in the editor (see screenshot), and not dynamically generated at runtime. alt text

I really have no idea what's going on here - as I said, the debugger is definitely showing that OpenMenu is getting the previous GameObject rather than the one that should be provided by the button clicked, so I'm pretty stumped. If anyone has seen anything like this or has any idea what's going on, help is greatly appreciated.

Thanks!

buttons.jpg (54.1 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nj4242 · Sep 15, 2016 at 06:35 AM

As what I can see here, Is that you are using UI button, and you want to trigger some events, and from what I understood, you want to call certain gameobjects on specific button click. Something like this If I am not wrong, click on Button A call/recieve object A, and similarly onClick button B and call/recieve object B, right @Icepick ?

Looking at the image you've given, I don't see any particular gameobject to call/recieve, so you might wanna provide some object related images, not mainmenu, you are having problems with objects but didn't provide anything related to objects. So, It's hard to see things clear.

Your objects (~sorry GameObejcts), must be identified, before calling it, so OnClick( ) should be able to know which object to call. So, The reason your OnClick( ) receiving same Game Object after Escape button, Is because Escape button Input function is not passed/set and you might not able to identify which object to call at OnClick( ).

Try to add this on your OpenMenu( ) function.

 if (Input.GetKey(KeyCode.Escape))
 {
   //Write the same code written on back UI button.
 }

There is also an Alterative way, as you're also doing activation and deactivation of the gameobjects in between triggers, so, This is what I suggest you :

 void OnMouseDown( )
 {
       anyGameObj.setActive(true); 
 }

This function actually catches specific click on a specified Game Object, its not work with UI objects, It'll need a collider attached and will identify which is clicked and what to do.

So, Basically for this to work you need gameobjects attached with a collider and script with this function on it, it will identify your button click dynamically, and always checks which gameobject is clicked, unlike OnClick ( ) which only identifies a click on the gameobjects.

Regards.

Comment
Add comment · 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
0

Answer by Icepick · Sep 15, 2016 at 03:47 PM

Looks like I was explaining the problem badly, but I've actually worked out the issue now. What was happening was that because the menu element containing the button was getting deactivated when it was clicked, the EventHandler basically thought the button was still selected, and it seems it couldn't change that since the button itself was now deactivated. Clicking through the buttons worked fine because you're obviously selecting a new button and all that, but pressing escape through everything off so that it thought a disabled button was still selected. So, what was happening was that after pressing escape to close a menu and open a new one, the system still thought the old button was selected, so the next click caused that button to click rather than the one you think you are clicking.

Basically, I solved the problem by adding this to the OpenMenu function GameObject myEventSystem = GameObject.Find("EventSystem"); myEventSystem .GetComponent().SetSelectedGameObject(null);


menu.jpg (138.9 kB)
Comment
Add comment · 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

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

UI: Execute action instead of "Navigation" 0 Answers

Problem with UI buttons and MultiTouch 4 Answers

Click/touch object to enable/disable buttons inside Canvas 1 Answer

Highlight images in buttons children 0 Answers

How do I have clickable buttons in a scroll rect? 1 Answer


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