Search Unity

Resolved How can you instantiate and select a selectable on the same frame?

Discussion in 'UGUI & TextMesh Pro' started by empika, Aug 26, 2022.

  1. empika

    empika

    Joined:
    Jul 12, 2012
    Posts:
    31
    Hallo,

    It seems impossible to instantiate a prefab with a Selectable component and then call
    .Select()
    on it in the same frame. Usually I have coroutine that waits a frame and then selects the given Selectable, this isn't ideal as the UI can sometimes feel a little laggy. Even at 60fps you can still notice.

    So ignoring some kind of pooling solution, how can you instantiate and select a Selectable on the same frame?

    Thanks
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Im not aware of any issues like you describe. I just made a simple project to see if I could reproduce it but it's working as expected for me.


    eg.gif

    This is the script I made:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class SelectTest : MonoBehaviour
    5. {
    6.     public GameObject prefab;
    7.     public GameObject root;
    8.  
    9.     [ContextMenu("Do IT")]
    10.     public void CreateAndSelect()
    11.     {
    12.         var instance = Instantiate(prefab, root.transform);
    13.         var selectable = instance.GetComponent<Selectable>();
    14.         selectable.Select();
    15.     }
    16. }
    Am I missing something?
     
  3. empika

    empika

    Joined:
    Jul 12, 2012
    Posts:
    31
    Well that's good, thanks Karl! It must have been an issue in earlier versions. This project has come up through 4.3 - 2020.3!
     
    karl_jones likes this.