0

I realize that the documentation says to avoid doing this if possible, but I need functionality that doesn't exist in any currently available Object subclass, I need a serialized data type (stored as JSON), that can be read from and written to file at runtime (for user created content / randomly created at runtime), which eliminates both TextAsset (cannot be edited) and ScriptableObject (cannot save outside the Editor Environment), this data will be tied to a specific extension. I know that it should be possible to make this work but I have no idea what needs to be done here. When I attempt to do this new Unit() { name = "Custom Unit", GivenName="John", FamilyName="Smith"}; Unity throws a null reference error when attempting to set name. Are there some special steps required when working with Object?

  • what do you mean by `cannot be edited`? You could probably implement a kind of Text editor window for editing the content of TextAssets directly within Unity. I'm almost sure this is something that already exists in the Assetstore – derHugo Dec 01 '21 at 15:28
  • All types derived from `UnityEngine.Object` are only representations in the `c#` layer API of actual underlying native objects in the `c++` backend of Unity. Therefore it doesn't really make sense to derive a custom type directly from `UnityEngine.Object` since the backend will no be aware of that type. – derHugo Dec 01 '21 at 15:32
  • Does this answer your question? [What is the best way to save game state?](https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state) – Ruzihm Dec 01 '21 at 16:21
  • You probably want to explore the idea of models - simple data objects that are serializeable (and don't inherit from MonoBehaviour). Your game objects will have a model reference, and at runtime, you load/save the models using traditional C# (JSON or binary, whatever you prefer) and attach them to your game objects. – Chris Dec 01 '21 at 19:19
  • @Ruzihm The idea, is that there will be premade (by me) units that will be used throughout the game, some that can join the player, and be edited as a result (which will save to the players save game), so unfortunately that doesn't really work, I can create the in-game loading/saving pretty easily since the players save file wouldn't be located within the compiled game (for obvious reasons), it's attempting to create a new file with the extension of ".unit" and being able to drag it to an inspector field. –  Dec 01 '21 at 23:43
  • @Chris Where would a well documented place to start learning about this? –  Dec 01 '21 at 23:44

0 Answers0