Skip to main content
4 votes
Accepted

ScriptableObjects lose references/stored objects upon build/restarting the Unity Editor?

It was due to having auto properties on serialized classes: public TileData TileData { get; private set; } Using properties with ...
Tudvari's user avatar
  • 801
3 votes
Accepted

How to reference nameof private serialized field in unity custom inspector

Use your interface Handily enough, you already have your answer. Leave the speed field public, but never pass around or declare references to that class. Only ever ...
Ed Marty's user avatar
  • 5,259
3 votes

Update dependent values of Serializable C# classes on Inspector

You can use a MonoBehaviour or ScriptableObject's OnValidate() method. This gets called in the Editor when a script instance gets (re)loaded or when Inspector properties are changed. It won't ...
DMGregory's user avatar
  • 141k
3 votes
Accepted

Serialization/Deserialization Solutions for Loading & Playing Audio Files

Unity internally uses ogg vorbis for audio - whenever you drop an mp3 into your Unity project, the editor converts it to vorbis and uses that. Importantly, this means Unity can natively decode ogg ...
Luke Briggs's user avatar
2 votes

Unitys [SerializeField] and parallel inheritance

It looks to me like you might actually want an IItem interface, or abstract class, a bit like this: ...
DMGregory's user avatar
  • 141k
2 votes

How can I create a custom PropertyDrawer for my Point struct?

I tried using the solution in the accepted answer but was having some issues with it, particularly when it was being used within a list. After messing around with things a bit and trying out different ...
Tallek's user avatar
  • 121
2 votes

Update dependent values of Serializable C# classes on Inspector

In my opinion I these variables should be treated separately. Use your properties methods as just interactor to that one variable alone. Putting logic in them is a bit smelly I think. The setter for ...
Matster2's user avatar
2 votes
Accepted

Saving and loading a GameObject's full state easily in Unity3D?

I think that you went down the wrong rabbit hole and should take a step back instead of going deeper. You goal is to generate levels proceduraly. For that you want predesigned rooms containing ...
Bartimaeus's user avatar
2 votes

Json Libgdx serialize / de-serialize a Object Map of Object Maps

libGDX Json converts Enums to strings and therefore cannot serialize / de-serialize Map keys to anything but strings. To fix this, either... Use strings for keys in Maps (ObjectMap, HashMap ect.). ...
Macmanmatty's user avatar
2 votes

Unity3D - JsonUtility returns empty string

JSONUtility can serialize fields, but it can not serialize properties. As the documentation of [SerializeField] says: The serialization system can do the ...
Philipp's user avatar
  • 123k
2 votes
Accepted

Error: "JSON must represent an object type" when parsing array of objects

When you write a line like this: LineData line1 = JsonUtility.FromJson<LineData>(Dialog_Data.text); You're saying "the JSON in this text string represents ...
DMGregory's user avatar
  • 141k
2 votes

Component instance reference in Unreal Engine 4

To do this in Unreal C++, inside the CPP file you need to do the following: Make sure to include the header component: #include "Camera/CameraComponent.h" Then in the class constructor: ...
Stephen's user avatar
  • 1,040
2 votes

How to prevent Unity from generating unnecessary changes after saving a scene with no actual changes

EDIT: This bug seems to have been fixed in newer versions of Unity (e.g. 2020 LTS and 2021 LTS). EDIT 2: This bug has returned in Unity 2022 and Unity 6 As far as I know, this is caused by a bug or ...
Kevin's user avatar
  • 7,029
2 votes
Accepted

Serializing nested ScriptableObjects with runtime changes

IIRC Unity won't serialize a List where the data type is abstract. Your FilesystemDirectory class has one field: ...
Kevin's user avatar
  • 7,029
2 votes
Accepted

What is the difference between Serializable, and System.Serializable?

Unless I'm mistaken, there is no difference - System.Serializable is just the fully qualified name of Serializable. C# has this ...
Filip Milovanović's user avatar
2 votes
Accepted

Text Only Dungeon Crawler game. Hardcoding Maps vs Generating Map Data externally?

I would recommend to use a standard markup format like XML, YAML or JSON for your files. There are plenty of C++ libraries available for each of them which make converting between text files and C++ ...
Philipp's user avatar
  • 123k
2 votes
Accepted

Serialize List of References

The Problem In Unity's Docs : [Serialize] Referenced values cannot be shared between UnityEngine.Object instances. For example, two MonoBehaviours cannot share an object that is serialized by ...
Charly's user avatar
  • 752
2 votes
Accepted

How to check if a Vector3 or Quaternion are unassigned?

If a struct type has not been given any particular value, it will usually default to a special value helpfully called default. (...
DMGregory's user avatar
  • 141k
2 votes

Loading game state before first save has occurred (chicken/egg problem)

The file system can fail beyond your control. If you have a default file, it might be removed due to fortuitous reasons. That might suggest to you that to check if the file exists is the way to go. ...
Theraot's user avatar
  • 28.3k
2 votes

Loading game state before first save has occurred (chicken/egg problem)

I made very good experiences with keeping savegames in JSON format. The reason is that you can easily view savegames for troubleshooting and manually create and edit savegames in a text editor to ...
Philipp's user avatar
  • 123k
2 votes
Accepted

Persist global shader parameter

We can adapt the example shown in the Unity documentation here to get something that should work for your use case. This still has the slight quirk that two instances of the ...
DMGregory's user avatar
  • 141k
1 vote
Accepted

Getting info about the script a [PropertyAttribute] is attached to in Unity

You want the declaring type of the field that uses the attribute, not the declaring type of the attribute itself. (The attribute type is defined in its own class file, so it has no tie back to the ...
DMGregory's user avatar
  • 141k
1 vote

Designing persistence in an ECS world subdivided into chunks

We had to solve a similar problem when developing Starlink: Battle for Atlas. The game takes place in a system of seven planets, and although only a small chunk of the current planet around the player ...
DMGregory's user avatar
  • 141k
1 vote

Designing persistence in an ECS world subdivided into chunks

Solution A (Alternative) seems reasonable given the constraints you mention in your question and I suggest giving it a try. Regarding your hesitancy to stray from an ECS; notice that there is a ...
loaner9's user avatar
  • 111
1 vote
Accepted

How can I serialize lists to JSON on mobile?

To be able to serialize some data using Unity's built in serializer, you will have to contain your data in a class. You can't simply serialize a list as is. Something like this should work. You can ...
ezez's user avatar
  • 326
1 vote

How can I serialize lists to JSON on mobile?

The JSON.NET plugin for Unity officially supports "all Unity platforms including WebGL, except for WebPlayer, Windows 8.0 and Windows Phone 8.0."
Kevin's user avatar
  • 7,029
1 vote

Extending a ScriptableObject to have a get property instead of a field, without losing serialized data

Here's a cool trick you can use: ...
DMGregory's user avatar
  • 141k
1 vote
Accepted

How do you de/serialize a monobehaviour class?

Typically, you don't. MonoBehaviour is not (de)serializable by JsonUtility.FromJson. Components only make sense when attached to ...
DMGregory's user avatar
  • 141k
1 vote
Accepted

Deserialize only some fields from a JSON file

It looks like your problem is just a typo. In the JSON, your array of robots is called "Robots" (uppercase R), but in your class you named it ...
DMGregory's user avatar
  • 141k
1 vote
Accepted

How to properly format classes for JSON deserialization with JSON.net for Unity?

First, a few minor tweaks that might depend on your JSON library (it looks like you're using the Newtonsoft JSON library, rather than the built-in Unity JsonUtility)...
DMGregory's user avatar
  • 141k

Only top scored, non community-wiki answers of a minimum length are eligible