Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

EventSystem.SetSelectedGameObject

Discussion in 'UGUI & TextMesh Pro' started by nventimiglia, Aug 26, 2014.

  1. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    Code (CSharp):
    1.     public class uGUISelectControl : MonoBehaviour
    2.     {
    3.         void OnEnable()
    4.         {
    5.             var es = EventSystemManager.currentSystem;
    6.             es.SetSelectedGameObject(gameObject, new BaseEventData(es));
    7.         }
    8.     }
    I would like to select my controls from code. Above I have an example of an Autoselect script that does not work. How can I solve this ?
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    So the StandaloneInputModule gets activated after 'OnEnable' is called (it's called on the first Update of the EventSystem), so if your code is called before it will be canceled out.
     
  3. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    I have modified the code to run after the first update. It is not working.

    Code (CSharp):
    1. [AddComponentMenu("Foundation/Databinding/uGUISelectOnEnable")]
    2.     public class uGUISelectOnEnable : MonoBehaviour
    3.     {
    4.         void OnEnable()
    5.         {
    6.             StartCoroutine(OnEnableAsync());
    7.         }
    8.  
    9.         IEnumerator OnEnableAsync()
    10.         {
    11.             yield return 1;    
    12.             yield return 1;
    13.             var es = EventSystemManager.currentSystem;
    14.             es.SetSelectedGameObject(gameObject, new BaseEventData(es));
    15.         }
    16.     }
    Edit
    Got it working, Fix was looking at the TabNavigation script. I was testing on an input control and I was not setting the carrot.
     
    Last edited: Aug 27, 2014