- Home /
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);
}
}
});
}
Answer by kaushiknis · Mar 16 at 02:53 PM
@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;
}
}
});
}
Answer by Narc0t1CYM · Mar 03 at 09:38 PM
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;
prefab is supposed to be the Tree-A-01 object, I have it attached already to the script gameobject.
public GameObject prefab;
this is how it is written in the script..
I tried your solution, I dragged the prefab to the Resources folder in Assets and then I called the path for it, but it still does not load.
Hello there,
could you run the game and check during runtime in the inspector tab if the prefab is still set at runtime? I mean, just to be sure. You could also debug the prefab right before instantiating it:
Debug.Log("Myprefab:"+prefab);
Then Assign the newly created object to a variable an check if it is null:
GameObject myNewTree = Instantiate(prefab, loadTreePosition, loadTreeRotation);
Debug.Log("Is my tree null? "+(myNewTree == null));
Can you send a screenshot about your Tree-A-01 prefab, please?
this is the path and inspector, sorry for some reason they arent letting me post this properly
they didnt let me post all the screenshots in one reply so here is the bottom half of the inspector as well.
I'm sorry, I'm not sure what's happening :\ Have you figured it out since then?
Your answer
Follow this Question
Related Questions
Destroying assets is not permitted to avoid data loss. 0 Answers
Instantiate is spawning too many clones 2 Answers
My objects instantiate at a strange z coordinate 0 Answers
How to instantiate particle effect after destroying it 2 Answers
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer