Hello, I need a little help. I'm having trouble loading JSON with serialization. I thought at first it was a problem with deserialization but after testing and checking I noticed that serialization stops at about the fifth object. I removed some data that JSON contains but serialization still stops at 5 object and I don't really know why. It's not a problem with a specific object because I've tried different ones. I would really appreciate some help if anyone has had a similar problem. I would appreciate any help.The hierarchy of the object (MainObject) I want to save looks like this:
#Gameobjects serialization problem
1 messages ยท Page 1 of 1 (latest)
How are u serializing it. It's likely you're just hitting the depth limit
Hard to say, can I show you code which is responisble for it?
{
SerializedGameObject serializedObj = new SerializedGameObject
{
name = obj.name,
tag = obj.tag,
layer = obj.layer,
isStatic = obj.isStatic,
parent = obj.transform.parent,
localPosition = new SerializableVector3(obj.transform.localPosition.x, obj.transform.localPosition.y, obj.transform.localPosition.z),
localRotation = new SerializableQuaternion(obj.transform.localRotation),
localScale = new SerializableVector3(obj.transform.localScale.x, obj.transform.localScale.y, obj.transform.localScale.z),
activeSelf = obj.activeSelf
};
foreach (Component component in obj.GetComponents<Component>())
{
if (component is Transform) continue;
SerializedComponent serializedComponent = new SerializedComponent
{
type = component.GetType().AssemblyQualifiedName
};
try
{
if (component is MeshRenderer)
{
serializedObj.meshRenderers.Add(SerializeMeshRenderer(component as MeshRenderer));
}
}
catch (ArgumentException)
{
Debug.LogWarning($"Could not serialize component of type {component.GetType().Name} on object {obj.name}");
}
}
foreach (Transform child in obj.transform)
{
serializedObj.children.Add(Serialize(child.gameObject));
}
return serializedObj;
}```
Did you write this
What you mean? THe code?
Yes
Yea, I did with some help
I'll be on pc in like 2 mins, hard to read on mobile
Ok, thank you 
can u show what this becomes in json
JSON HAS LIKE 61K characters ๐
yea this is most likely some json limit then, because i see you're just looping over the objects
So I have prefabs and you can connect them to eachother So each prefab has connection point which next prefab can connect to
So if there is 7 prefabs there is already 7 layers :/
you may want to reference these differently, so there arent a possible infinite amount of connections
like give an ID to what prefab is connected instead
you really dont want to save entire gameobjects like this
like for example do you really need to save every single component
Hmm no idea tbh. I found out about this solution few weeks back and now I need to fight with it again. I would be more than happy to just serilize which prefab and what parent the prefab has instead of doing each component
If it is possible doing like that
yea i really wouldnt use this
But still I would need to save the materials and some of the variables from scripts that are inside of the prefab
your prefabs are already saved pretty much in the game files, unless the user has complete control over editing the materials then theres really no point in saving them
User can change the material/color of the prefabs model in the scene but the prefab in the assets is still the same
Sorry if I don't make sense it is hard for me to explain so of the things XD
It is confusing af
But there are predefined materials so there is no custom colors or anything
idk its hard for me to say what you can cut out, for example in my game i have a player prefab and the player can control its own material colors/metallic/smoothness variables, so i just save those only. Load the default prefab into the game, then apply the saved material
you likely dont need to be saving entire game objects, maybe just its position and an ID for what it connects to
Hard to say, I will try to do that. I hope it will not be too much pain to do something like that.
I am not fan of deserialization
But cannot do really anything else when I am using Unity WebGL and connecting it to MySQL
well you're saving to json already lol, doesnt matter what you save, you have to retrieve it somehow
It is retriving
then what i suggested is the same thing
It is just the serialization is not getting all of the objects in the hierarchy
webgl and mysql, you definitely arent gonna be saving a 61k line json file
you should just save the parts you need only
So in my case, Position,rotation, parent, material, prefab reference or smth, is that what you are saying instead of going all components?
maybe not even parent, but yea
like it really depends what these prefabs are, the example i gave was my actual player prefab which is like 50 transforms in total. I still only save the material because the prefab doesnt change
if i need to, i can save the location of the main transform but my game doesnt require it
I have 9 prefabs they look like this
When it come to hierarchy
Then Object 2 goes under SnapPoint and so on
model has renderer and materials
i mean i still dont know what this actually is, its more for u to decide what you actually need saved
does the transforms ever get changed, do the childs local transforms ever change, does scale/rotation ever change, its activeself?
Only Main parent Object1, object2 etc get the position and rotation changed the rest just stays in place by following the rotation,position of the parent
Scale is always the same
And the materials of model, and knobs are getting changed and that is all tbh
So I think I know what I need to save now ๐
I just hope accessing everything inside the prefabs and applying everything will not be pain to do
itll be more annoying in a sense, but saving entire game objects just isnt viable like this
Wait which is not viable? The thing that I have right now or the one that we are talking about ?
the thing u have right now
saving what u need then applying it is way less data saved
and in reality its not even gonna be a performance hit unless u are doing this to a ton of objects
but loading a massive json/sql is way more of a performance hit i think
Understood, So I will have to change a lot of stuff ๐
Hope it will go smooth
Thank you very much for help for now, I will try what I can do with the new understanding :D. But firstly need to go to sleep since it is getting a bit late ๐
Is it okay if I could ping you If I will have any question?
try just asking in the channels, if i see it ill answer