- Home /
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");
}
}
}
The two problems:
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
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?
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.
Hi SimRuJ, unfortunately I didnt manage to find solution... (yet)
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"
}
}
Your answer
Follow this Question
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