prefab not loading with instantiate

I am trying to instantiate a prefab based off position and rotation values saved to firebase, but for some reason the prefab is not instantiating even though I am able to retrieve the values from firebase.

Here is my method, it is attached to a button that is supposed to load in all the prefabs onclick

I tried Resource.Load(), The prefab is active, I also have the prefab attached to the game object with the script attached to it.

The values in the Vector3 and Quaternion are also correct since they debug properly.

Also there are no errors when I load the game.

public void GetTrees()
    {
        FirebaseDatabase dbInstance = FirebaseDatabase.DefaultInstance;

        dbInstance.GetReference("Player").Child(scenarioName).GetValueAsync().ContinueWith(treeData =>
        {
            if (treeData.IsFaulted)
            {
                Debug.LogError("Error");
            }
            if (treeData.IsCompleted)
            {
                DataSnapshot snapshot = treeData.Result;

                foreach (DataSnapshot tree in snapshot.Children)
                {
                    IDictionary loadTreeDictionary = (IDictionary)tree.Value;

                    //Debug.Log(loadTreeDictionary.Values.Count); //returns 7 meaning all the data is in here..

                    xPos = float.Parse(loadTreeDictionary["xPos"].ToString());
                    yPos = float.Parse(loadTreeDictionary["yPos"].ToString());
                    zPos = float.Parse(loadTreeDictionary["zPos"].ToString());

                    xRot = float.Parse(loadTreeDictionary["xRot"].ToString());
                    yRot = float.Parse(loadTreeDictionary["yRot"].ToString());
                    zRot = float.Parse(loadTreeDictionary["zRot"].ToString());
                    wRot = float.Parse(loadTreeDictionary["wRot"].ToString());

                    Vector3 loadTreePosition = new Vector3(xPos, yPos, zPos);
                    Quaternion loadTreeRotation = new Quaternion(xRot, yRot, zRot, wRot);

                    //Debug.Log(prefab.name); //it does not even debug the prefab name

                    //Debug.Log("Tree Positons " + loadTreePosition);
                    //Debug.Log("Tree Rotations " + loadTreeRotation);

                    Instantiate(prefab, loadTreePosition, loadTreeRotation);
                }
            }
        });
    }

@yubert103 This might be happening cause of async operation. Can you try Instantiating prefab from unity thread i.e. calling it from update method something like that. Just pasting a example here, this is not the correct way to do this but at least you’ll be sure of the issue.

What I did here is updating a boolean variable from GetTrees() method and based on the value instantiating the prefab from update method.

bool valueUpdated = false;

    private void Update()
    {
        if (valueUpdated)
        {
            Vector3 loadTreePosition = new Vector3(xPos, yPos, zPos);
            Quaternion loadTreeRotation = new Quaternion(xRot, yRot, zRot, wRot);

            //Debug.Log(prefab.name); //it does not even debug the prefab name

            //Debug.Log("Tree Positons " + loadTreePosition);
            //Debug.Log("Tree Rotations " + loadTreeRotation);

            Instantiate(prefab, loadTreePosition, loadTreeRotation);

            valueUpdated = false;
        }
    }

    public void GetTrees()
    {
        FirebaseDatabase dbInstance = FirebaseDatabase.DefaultInstance;

        dbInstance.GetReference("Player").Child(scenarioName).GetValueAsync().ContinueWith(treeData =>
        {
            if (treeData.IsFaulted)
            {
                Debug.LogError("Error");
            }
            if (treeData.IsCompleted)
            {
                DataSnapshot snapshot = treeData.Result;

                foreach (DataSnapshot tree in snapshot.Children)
                {
                    IDictionary loadTreeDictionary = (IDictionary)tree.Value;

                    //Debug.Log(loadTreeDictionary.Values.Count); //returns 7 meaning all the data is in here..

                    xPos = float.Parse(loadTreeDictionary["xPos"].ToString());
                    yPos = float.Parse(loadTreeDictionary["yPos"].ToString());
                    zPos = float.Parse(loadTreeDictionary["zPos"].ToString());

                    xRot = float.Parse(loadTreeDictionary["xRot"].ToString());
                    yRot = float.Parse(loadTreeDictionary["yRot"].ToString());
                    zRot = float.Parse(loadTreeDictionary["zRot"].ToString());
                    wRot = float.Parse(loadTreeDictionary["wRot"].ToString());

                    valueUpdated = true;
                }
            }
        });
    }

What is prefab in this context? I can’t find the reference of it. The first property of Instantiate() should be an Object as per the documentation says: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html.

What you often do in a situation like this, is that you pass a GameObject via Resources.Load(). The only parameter of Resources.Load() is a string which is the path to your prefab in the Resources folder (https://docs.unity3d.com/ScriptReference/Resources.Load.html).

If you don’t have a Resources folder yet, then create one and in the root directory and put your prefab in it. If you also want to save the reference into a variable then last thing you need to do is, you have to cast it to a GameObject

So your Instantiate() should look like this:

  Instantiate(Resources.Load("path/to/your/prefab"), loadTreePosition, loadTreeRotation) as GameObject;