Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yubert103 · Mar 03 at 07:59 PM · instantiateprefabdatabaseinstantiate prefab

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);
                 }
             }
         });
     }


alt text

tree-data.png (18.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
                 }
             }
         });
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yubert103 · Mar 03 at 10:06 PM 0
Share

alt text

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.

inspector.png (39.6 kB)
avatar image Anox yubert103 · Mar 16 at 11:12 AM 0
Share

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));
avatar image Narc0t1CYM · Mar 04 at 05:34 PM 0
Share

Can you send a screenshot about your Tree-A-01 prefab, please?

avatar image yubert103 Narc0t1CYM · Mar 04 at 06:32 PM 0
Share

alt text

this is the path and inspector, sorry for some reason they arent letting me post this properly alt text

treeinspector.png (63.8 kB)
treeresource.png (19.6 kB)
avatar image yubert103 Narc0t1CYM · Mar 04 at 08:03 PM 0
Share

alt text

they didnt let me post all the screenshots in one reply so here is the bottom half of the inspector as well.

treeinspector2.png (79.5 kB)
avatar image Narc0t1CYM yubert103 · Mar 16 at 10:53 AM 0
Share

I'm sorry, I'm not sure what's happening :\ Have you figured it out since then?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

195 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Problem when assigning a prefab 1 Answer

Why is it important to create an empty gameobject for my prefabs? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges