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

Bug Object.InstantiateAsync not working properly

Discussion in 'Scripting' started by Furgl, Apr 2, 2024.

  1. Furgl

    Furgl

    Joined:
    Jan 6, 2022
    Posts:
    25
    I'm using this code to test the differences between Instantiate and InstantiateAsync:
    Code (CSharp):
    1. GameObject go = Instantiate(prefab, transform);
    2. go.name = "Instantiate Result";
    3.  
    4. var async = InstantiateAsync(prefab, transform);
    5. async.completed += operation => {
    6.    GameObject go = async.Result[0];
    7.     go.name = "Instantiate Async Result";
    8. };
    My prefab is fairly complicated with custom serializable dictionaries (using ISerializationCallbackReceiver) and many meshes (added to the prefab asset). Using Instantiate works perfectly; using InstantiateAsync does not:

    The async result does not copy the serializable dictionaries from the prefab. By putting in debug prints in OnBeforeSerialize() and OnAfterDeserialize(), I see that only OnBeforeSerialize() is called when InstantiateAsync is used (Instantiate calls both properly). I can work around this by manually calling OnAfterDeserialize() on every serializable dictionary, but that's not ideal.


    My prefab has GameObjects with LOD Groups that use the GameObject's LOD children as Mesh Renderers. Instantiate properly links the renderers to the LOD children, but the async result still links the renderers to the LOD children in the prefab asset (which is in an Addressable, which you can see in the screenshot). This would not be easy to make a workaround for since my prefabs have many GameObjects with LOD Groups that would need to be reconnected to their LOD children at runtime.
     
  2. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    116
    bump. feature breaking bug
     
  3. ZERO025

    ZERO025

    Joined:
    Jun 3, 2015
    Posts:
    5
    Have the same problem in unity 2022.3.23f1
     
  4. Furgl

    Furgl

    Joined:
    Jan 6, 2022
    Posts:
    25
    I submit a bug report IN-72997 with a basic project that demonstrates the issues.
     
    eggsamurai likes this.
  5. Furgl

    Furgl

    Joined:
    Jan 6, 2022
    Posts:
    25
    ZERO025 and eggsamurai like this.