1

Scriptable Objects can be used as data containers in Unity3d. They don't need to be attached to a GameObject in a scene but they can be saved as assets in our project. Most often, they are used as assets which are only meant to store data.

We can do create and add data as below.

[CreateAssetMenu(fileName ="New Player Card" , menuName ="Card")]
    public class GameData : ScriptableObject
    {
        public static string name;
        public int health;
        public spawnDelay;
    }

And then we can create assets by Create->Card.

What I was looking to do is instead of creating assets to the game, create assets based on the data during the gameplay its own. And Destroy them at the end of the game or the scene. Does anybody have an idea of doing this? Any guide/ ideas would be highly appreciated. Thank you.

  • 1
    ScriptableObject is only good for such use that you store default values and data to it, sort of like keep it as a storage location for your preset values. For runtime changes you need to write your c# class with your desired behaviour. You can set the default values in the Start() or Awake() if its monobehaviour or in the c# class constructor otherwise. – rustyBucketBay Mar 11 '20 at 07:55
  • Thank you for the comment @RustyBucketBay, On behalf of my requirement, I tried to do what you said already, which made my game inconvenient and high memory usage because its rapidly changing scenes. Can I keep One scriptable object for each and rewrite data? – Sakuna Madushanka Mar 11 '20 at 08:02
  • 1
    You can make a scene that does not destroy between loads. In al gameobject of that scene you can keep and change your data. Also you can avoid gamobject destruction between scene loads: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html – rustyBucketBay Mar 11 '20 at 08:05
  • 1
    @RustyBucketBay, nice, I will try and let you know. If you could, post it as an answer please. thank you. – Sakuna Madushanka Mar 11 '20 at 08:08

1 Answers1

0

ScriptableObject is only good for such use that you store default values and data to it, sort of like keep it as a storage location for your preset values. For runtime changes you need to write your c# class with your desired behaviour. You can set the default values in the Start() or Awake() if its monobehaviour or in the c# class constructor otherwise.

ou can make a scene that does not destroy between loads. In al gameobject of that scene you can keep and change your data. Also you can avoid gamobject destruction between scene loads:

docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47