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

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

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.

8393130--1107675--eg.gif

This is the script I made:

using UnityEngine;
using UnityEngine.UI;

public class SelectTest : MonoBehaviour
{
    public GameObject prefab;
    public GameObject root;

    [ContextMenu("Do IT")]
    public void CreateAndSelect()
    {
        var instance = Instantiate(prefab, root.transform);
        var selectable = instance.GetComponent<Selectable>();
        selectable.Select();
    }
}

Am I missing something?

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!

1 Like