I tried to solve this problem for a while now and can’t find a proper solution and get more and more confused about the whole theory behind it.
I’ve read hundreds of different articles and forum topics about it but couldn’t find a solution.
Maybe i tackle this problem in the complete wrong way…I’m open for any suggestions!
So my problem is the following. I’ve created a binary formatting save systems which works fine.
It saves a playerData File that can be properly loaded and is persitent through scenes.
The PlayerData holds a List of CreatureData (i don’t like the comparison but yeah think about pokemon)
when entering a battle i have to recreate the Creatures from the saved Data (all saved Data is serialized)
I didn’t expect to get problems with the re-Instantiation of the Gameobjects.
A battle participant was planned to consist out of a
-prefab (A Mesh with Animations)
-CreatureTemplate (Base Informations for all creatures of that type) as scriptable object
-CreatureData (Current individual values for this specific creature) This is what is saved in the playerData file
i need this structure because every creature will have the same template but will develop individually throughout it’s levels.
I planned to use Resources.Load with a string id from the creatureData to load the referenced scriptable object asset(which again holds the prefab))
But how ever i try this i always get back a null.
So is there already a flaw in that logic? how should i try to recreate a gameobject from the data i can serialize?
which returns Creatures/Larva for example and should direct to my file located in Assets/Resources/Creatures/Larva.asset I guess the problem is somwhat in the typeof. currently im simply looking for an object adn try to cast it into a gameobject so can can use a getcomponent to get on my CreatureData…i tried to look for creaturedata in the beginning but couldnt bring that to work either… and received “cannot convert 'unityEngine.Object” to “CreatureData” via built-in conversion…so i guess this means that my custom asset is considered an Object. i tried tons of stuff but i’m totally stuck here
I tried your code snippet but i get the exact same error (can’t cast from Unity.Object to CreatureData)
it happens at the typeof Passage.
I’m still absolutly lost and it get’s worse and worse each day.
A friend of mine suggested to restructure it a bit and simply save a reference to the scriptable object.
I tried and failed miserably.
when i try to save a reference to my scriptable object i get a serialization error no matter how i structure it.
when the scriptableobject isn’t serialized i get a (serialization missing error) so i guess every object that is somehow referenced in my saveFile also has to be serializable
when i serialize the scriptableObject to i get a huge error message that doesn’t tell me anything
(serialization exception type unityEngine.ScriptableObject in assembly UnityEngine is not marked as serializable)
Brickwall for me. I have no Idea why i can’t serialize it…
So i’m back to the idea of using a string or int to find my scriptableobject reference at runtime… here is a small graph of what im trying to achieve.
I just received the right hint for something like this! In case anybody else might need this.
If you have a scriptableObject as an Asset Instance you have to use AssetDatabase.LoadAtPath and everything works like a charm. maybe i will stumble upon some further problems with this but for now atleast it seems to be working