Prefab references

Discussion and help for Easy Save 3
Post Reply
Bacall
Posts: 2
Joined: Thu Jan 06, 2022 1:55 pm

Prefab references

Post by Bacall »

I want to load prefabs into a List of GameObjects.When I Load the prefab, it instantiates the game object in the scene (which I know is supposed to happen from the docs). I saw in one of your replies to a forum posts “you can’t load a reference to a prefab which doesn’t exist”, prefab which doesn’t exist in the scene?.
My question: Is it possible to load prefab references into a List, without instantiating game objects (like I dragged them there from the Unity Editor, Project Window). Or would I need to use some other way of doing my system, because since the prefabs aren't in scene, they'll always instantiate on Load?

I figured I use LoadInto, but the list is still empty. And if there's already something in the list I get an error:
IndexOutOfRangeException: The collection we are loading is longer than the collection provided as a parameter.
ES3Types.ES3ListType.ReadInto (ES3Reader reader, System.Object obj) (at Assets/Plugins/Easy Save 3/Scripts/Types/Collection Types/ES3ListType.cs:100)
ES3Reader.ReadInto[T] (System.Object obj, ES3Types.ES3Type type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:288)
ES3Reader.ReadInto[T] (System.String key, T obj) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:228)
ES3.LoadInto[T] (System.String key, T obj, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:532)
ES3.LoadInto[T] (System.String key, T obj) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:487)
PlayerSaveAndLoad.LoadInventory () (at Assets/Scripts/Characters/Player/PlayerSaveAndLoad.cs:163)
PlayerSaveAndLoad.LoadAll () (at Assets/Scripts/Characters/Player/PlayerSaveAndLoad.cs:123)
PlayerSaveAndLoad.Start () (at Assets/Scripts/Characters/Player/PlayerSaveAndLoad.cs:44)
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Prefab references

Post by Joel »

Hi there,
My question: Is it possible to load prefab references into a List, without instantiating game objects (like I dragged them there from the Unity Editor, Project Window).
I'm not sure I understand. When you drag a prefab from the Assets window into a scene, all Unity is doing is instantiating that prefab in your scene. Could you clarify what you mean by a prefab reference?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Bacall
Posts: 2
Joined: Thu Jan 06, 2022 1:55 pm

Re: Prefab references

Post by Bacall »

In the inspector, I have a visible List. Whatever prefabs I drag in there will load to the Player when I hit "Play". During runtime, I can collect even more weapons to the list. Is there a way to save this list? When I use ES3.Load, it instantiates prefabs into the scene, instead of only loading them to the list.

Image
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Prefab references

Post by Joel »

I see, thanks for clarifying.

This is because saving a list of prefabs will store them by value. If you want to store them by reference, the easiest way would be to create a class with the List as a field and save this class. Fields of Unity reference types are stored by reference.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply