Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Goubermouche · Aug 31, 2020 at 01:22 PM · prefabsloadingsavingresources.loaddirectory

how to instantiate saved objects back into game

hi! I'm currently saving my prefabs into Assets/Resources/nameofprefab and I would like to load and instantiate them back into the game (I'm trying to make a very basic prefab load/save solution) I have been told to use Resources.Load, however, I have not been able to make it work - can anyone give me any advice on this problem?

thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MHernandezOlmo · Aug 31, 2020 at 01:41 PM

Hi @Goubermouche, let's see if this helps you. Two easy ways for this:

1.-You can easily have a reference to your prefab on any script with a GameObject variable public, then drag the prefab to that script in the inspector and then instantiate itwith something like this:


alt text


     public GameObject targetPrefab;
     void Start()
     {
         Instantiate (targetPrefab);
     }
 



2.-If you want to load them into a variable and then instantiate, just do something like this: Imagine your prefabs is under "Resources" folder, then under a folder called "Prefabs" and you called it "MyPrefab". To load it into a variable, you can just do:

 GameObject targetPrefab = Resources.Load<GameObject>("Prefabs/MyPrefab");

And then do the instantiate think like above.

Hope this helps.


prefabedit.png (7.6 kB)
Comment
Add comment · Show 4 · 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 Goubermouche · Aug 31, 2020 at 06:45 PM 0
Share

hi! first of all - thanks for replying! second of all - I have tried the proposed solution however I was not able to make it work - it doesn't give me an error but it leaves the game object variable empty - any idea of what I'm doing wrong?

 // Start is called before the first frame update
 void Start()
 {
     //load objects
     GameObject gmb = Resources.Load<GameObject>(dataPath + "1");
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 2.0f;       
         Vector3 objectPos = Camera.main.ScreenToWorldPoint(mousePos);
         var newObject = Instantiate(obj, objectPos, Quaternion.identity);
         newObject.name = index.ToString();          
         index++;
         newObject.transform.parent = transform;
         objectsToSave.Add(newObject);
     }
     //clear save file
     if (Input.GetKeyDown(KeyCode.Delete))
     {
         Directory.Delete(dataPath, true);
         Directory.CreateDirectory(dataPath);    
     }
 }

 private void OnDestroy()
 {
     //save objects into the save file
     if(objectsToSave.Count > 0)
     {
         for (int i = 0; i < objectsToSave.Count; i++)
         {
             string localPath = "Assets/Resources/" + objectsToSave[i].name + ".prefab";    
             PrefabUtility.SaveAsPrefabAsset(objectsToSave[i], localPath);
         }
     }
 }

}

avatar image tonialatalo Goubermouche · Aug 31, 2020 at 07:05 PM 0
Share

$$anonymous$$aybe your path is wrong - the docs say that in that case it does not raise an error, but just returns null: "If an asset can be found at path, it is returned with type T, otherwise returns null" in https://docs.unity3d.com/ScriptReference/Resources.Load.html

avatar image Goubermouche tonialatalo · Sep 01, 2020 at 06:10 PM 0
Share

im dont think that is the issue since Im using datapath

Loading:

object gmb = Resources.Load("Assets/Resources/SaveFile/1.prefab"); Debug.Log(gmb);

I'm now getting a null value but i have no idea what I'm doing wrong

.![alt text][1] [1]: /storage/temp/166708-poznamka-2020-09-01-200748.jpg

poznamka-2020-09-01-200748.jpg (4.2 kB)
poznamka-2020-09-01-200811.jpg (20.5 kB)
Show more comments

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

144 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

Related Questions

Problem with showing "infinite saves" menu 0 Answers

How to prevent loading from freezing the game ? 2 Answers

Save Mic input and have it load when game starts up again 0 Answers

Some help with saving and loading in Unity 1 Answer

how to properly save an int value ? 2 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