Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question EventSystem.current.currentselectedgameobject not returning anything?

Discussion in 'Scripting' started by corypi, May 30, 2023.

  1. corypi

    corypi

    Joined:
    Jun 9, 2022
    Posts:
    34
    hi guys im having a issue with the event system returning a value to me.
    in my script i have
    Code (CSharp):
    1. private bool IsSelected(Button targetButton)
    2.     {
    3.         Debug.Log("entered isSelected() the button sent to it is " + targetButton + "its name is " + targetButton.name);
    4.         Debug.Log("the button that the event system thinks is selected is " + EventSystem.current.currentSelectedGameObject);
    5.         if (EventSystem.current.currentSelectedGameObject == targetButton)
    6.         {
    7.             Debug.Log("button is selected");
    8.             return true;
    9.         }
    10.         else
    11.         {
    12.             Debug.Log("button is not selected");
    13.             return false;
    14.         }
    15.     }
    the debug.log on line 4 of the above code block prints out the message and then a blank space where the selected game object should be.(highlighted line)
    Capture1.JPG
    however in my inspector i am seeing under the selected field
    Capture.JPG
    i thought about trying to add .gameobject to the debug.log to try to see if that was my issue and also tried .gameObject.name but when i do that i get a null reference

    not sure if this is a bug or im incorrectly accessing the selected object from the eventsystem
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    A
    GameObject
    instance will NEVER equal a
    Button
    instance.

    Do you perhaps intend to compare it to
    targetButton.gameObject
    ??
     
    Last edited: May 30, 2023
  3. corypi

    corypi

    Joined:
    Jun 9, 2022
    Posts:
    34
    i added the .gameobject to it and still it does not work as expected. interestingly enough if i have this code snippet inside of my selectButton method
    Code (CSharp):
    1. if (EventSystem.current.currentSelectedGameObject == null)
    2.         {
    3.             Debug.Log("im null");
    4.         }
    5.         else
    6.         {
    7.             showMeIt = EventSystem.current.currentSelectedGameObject;
    8.             Debug.Log("showMeIt should have recieved a game object");
    9.         }
    i receive get no game object from it but i tried to run it inside of update and it correctly pulls the game object. haven't done further testing but i believe i will beable to keep track of which object is selected in update() and then use that as a reference inside the select button method. not sure why it doesn't work outside of the update loop though.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Make sure one of them isn't a prefab on disk because that also would NEVER equal what is in your scene.

    Not sure what exactly you are trying to do here but there are interfaces you can implement for this stuff such as
    ISelectHandler
    interface.

    That's generally better than testing for equality.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. // @kurtdekker - stick it on a Selectable, such as a Button
    5.  
    6. public class ReportSelected : MonoBehaviour,ISelectHandler
    7. {
    8.     public void OnSelect(BaseEventData eventData)
    9.     {
    10.         Debug.Log("Foo:" + eventData.selectedObject);
    11.     }
    12. }
     
  5. corypi

    corypi

    Joined:
    Jun 9, 2022
    Posts:
    34
    the event system auto map feature was selecting buttons in the "pause menu" while my "optionsmenu" was active so i was trying to manually map them by iterating through a list of the available buttons whenever the user has pressed up or down respectively.

    my mainmenu script sets the optionsmenu gameobject in the canvas as active and from that i fire off a getbutton() method in the ButtonSelector class which holds the actual list of buttons that are in the scene.

    i thought it would be smart to check if the user has somehow selected a button and i found the currentSelectedGameObject in the eventsystems reference so i was attempting to use that to check the list of buttons to make sure one is not already selected so i could correctly assign the button index value to availableButtons[buttonIndex].select() and select the next or previous button depending on the currently selected button.

    i think it may be how my scripts are jumping from one to the other as even when i attempt to use the ISelectedHandler interface inside the buttonselector class still does not trigger the debug message that i threw into it for testing....
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You do know that the UI system supports both explicit and automatic focus navigation, right??

    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-SelectableNavigation.html

    You just make the UI and largely it works "as you'd expect..."
     
  7. corypi

    corypi

    Joined:
    Jun 9, 2022
    Posts:
    34
    yes i set all to none as i couldn't figure out how to drag the little arrows around when set to automatic which was selecting the buttons behind the options menu(i like the transparency effect from the stacked menus but it does actually work on automatic if i deactivate the menu that is behind the options) and then if i set it to explicit it only gave me two options for the up and down which now im realizing is more then likely the button i want to select from that button if it is selected.

    which will more then likely will solve my issue however it doesnt answer why the interface and the eventsystem.current are not returning a debug message or the currently selected game object... however on further investigation it does seem to be getting them a frame later going to see if it gets them right away from the mainmenu script then i should beable to just pass it from that script to the buttonselector script
     
  8. corypi

    corypi

    Joined:
    Jun 9, 2022
    Posts:
    34
    ok i think i figured out how to achieve what i was attempting.

    im pretty sure the issue was that my game object "MenuNavigationThroughInputSystem" that resides outside of the canvas(which also gets moved to my dontdestroyon load scene as my next goat is to have it automatically populate the avalableButtons list) cannot get access to the EventSystem immediately(it seems to take 4 frames until it can correctly pull the gameobject from it).

    however if i try to access my eventsystem from inside my mainmenu script which does live inside the canvas i get the game object back instantly so i can pass it into my buttonSelector class and reference it inside of the IsSelected method
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You don't. They just tell you where navigating on each element will take you.

    Hence 'automatic'.
     
    Kurt-Dekker likes this.