Search Unity

Get GameObject Clicked - Event Fails

Discussion in 'Scripting' started by mholmes, May 17, 2021.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I'm trying to get the GameObject that was clicked using Event System.

    upload_2021-5-17_11-27-40.png

    That is my GUI. I'm displaying a Panel. The button lives on a panel, not that should effect anything. Button click triggers this event:

    Code (CSharp):
    1.  public void Click_Crafting_Option()
    2.     {
    3.         string Name = EventSystem.current.currentSelectedGameObject.name.ToString();
    4.  
    5.         switch (Name)
    6.         {
    7.  
    8.         }
    9.     }
    I'm confused because I've used this code many times in the past without errors. Now I'm getting an error. Its saying my Selected Game Object is null. It should be this button. Ideas why this is null?
     
  2. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Figured it out. You have to setup Navigation or event system will not work. I just se it to Auto and worked