Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

how to instantiate addressables as CHILD and get the reference?

Discussion in 'Addressables' started by mailfromthewilds, Apr 21, 2020.

  1. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    215
    Code (csharp):
    1. GameObject FK = Instantiate(fakeSword);
    2.  
    3. Addressables.InstantiateAsync("Assets/___Blender/BlenderCube.blend", FK.transform);
    4.  
    5. FK.GetComponent<SomeScript>().target = FK.transform.GetChild(0).gameObject;
    thing is "FK.transform.GetChild(0).gameObject;" doesnt work. I instantiatedAsync as child but then i couldnt get the reference to it.

    doing Child(0) from coroutine works (after small time, like half second) but i cannot do it this way
    is there any other way to doit?

    i mean child(0) works but seems the InstantiateAsync is not fast enough, i mean the next line will execute before the child(0) gets spawned
     
  2. Rygielsiu

    Rygielsiu

    Joined:
    Mar 3, 2020
    Posts:
    2
    Hi, @mailfromthewilds

    Use:

    Code (CSharp):
    1. Addressables.InstantiateAsync("Assets/___Blender/BlenderCube.blend", FK.transform).Completed += OnLoadDone;
    2.  
    3. private void OnLoadDone(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> obj)
    4. {
    5. GameObject myGO = obj.Result;
    6. myGO.transform.parent = FK.transform;
    7. FK.GetComponent<SomeScript>().target = FK.transform.GetChild(0).gameObject;
    8. }
    What it does is it launches the code in OnLoadDone upon completition of the Async operation. I have not tested this exact example but I have used similar solution in my project and it works without any issues. Good luck!
     
    Ismaeel370 likes this.