Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
1
Question by jana-jarecki · Jul 27, 2016 at 07:11 PM · c#eventsystembutton trigger events

Problem with Navigation Mode & EventSystem.current.currentSelectedGameObject

My goal: Getting the name of a button (say "Option A" and "Option B") that is clicked without having a navigation on the button.

My approach: Clicking either button executes the function OnButtonClick() from the following script which is stored in the parent object of the two buttons. This works fine only as long as the Navigation of the button is not set to none.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class EventManager : MonoBehaviour
 {
     public void OnButtonClick()
     {
     var gameObject = EventSystem.current.currentSelectedGameObject;
     if (gameObject != null) {
         Debug.Log(gameObject.name);
         } else {
         Debug.Log ("No Option selected");
         }
     }
 }




alt text

The two problems:

  1. Setting the Navigation to none causes the retrieval of the name of the current selected game object to fail with the following error message

    NullReferenceException: Object reference not set to an instance of an object

  2. I need the name of the button to be retrieved every time the mouse clicks the button. Currently it retrieves the name only on the very first click of each of the buttons. Then the console prints the name of which ever button was pressed last.

The reason for setting the navigation to none is that any other type of navigation setting leads to the following behavior after clicking the button: Instead of displaying the Default sprite image of the button, what is displayed after clicking is the Highlighted Sprite image (here the "HighlightFilled" sprite).

I need to button to look as if it were never clicked after it was clicked. This only works with navigation set to none.

Any idea how to solve this?

unbenannt-2.jpg (36.3 kB)
Comment
Add comment · Show 3
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 SimRuJ · Apr 18, 2018 at 03:06 PM 0
Share

Did you manage to fix it or find a workaround? I've run into the same problem (1.) in Unity 2017.3.1f1:

With the navigation of a button set to "none", "EventSystem.current.currentSelectedGameObject" always returns "null". You get the actual button with the navigation on "automatic" but then the buttons keeps its highlighted color until you click on a different button.

avatar image jana-jarecki · Apr 18, 2018 at 03:24 PM 0
Share

Hi SimRuJ, unfortunately I didnt manage to find solution... (yet)

avatar image SimRuJ jana-jarecki · Apr 19, 2018 at 08:52 AM 0
Share

I found a workaround, it's not pretty and it'll be "fun" if you have a lot of buttons but at least it works with the navigation on "none":

  • Create a panel/canvas and make all of your buttons its children (or use an existing one)

  • Create a new script for the panel/canvas

  • Create a public variable in the script and an "onClick" method for it

  • Pull the actual button object onto the newly created field in the Inspector (will take a couple of seconds to show up after saving the file), now you can actually use it in your script

  • Create a new "onClick" event for the button (in the Inspector), pull the game object containing the script on it and set the method in your script in the drop down menu

The code would be:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ButtonScript : $$anonymous$$onoBehaviour {
     public GameObject myButton;
 
     public void ClickButton() {
         //Do stuff with "myButton"
     }
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trying to make a button interactable 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

event trigger error 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