Filip
Filip

Reputation: 23

Is it the right way to reference other script if it doesn't have MonoBehaviour?

I'm making a saving system for my mobile game. I have a script for economy that tracks the amount of in-game currency and i'd like to reference a non MonoBehaviour script that will hold the data to save

public class Money : MonoBehaviour
{
        public int Gold;
        public int Platinum;
        public int Tokens;
        public DataHolder data;
    
        private void Update()
        {
            data.Platinum = Platinum;
            data.Tokens = Tokens;
            data.Gold = Gold;
        }
}

public class DataHolder
{
    public int[] dragonLevel;
    public bool[] dragonMasterLevel;
    public int Gold;
    public int Platinum;
    public int Tokens;


}

Will unity automatically get the reference and will it properly transfer this data?

Upvotes: 1

Views: 323

Answers (2)

varunkaustubh
varunkaustubh

Reputation: 351

Adding to the other answer, you could just create an object of your DataHolder class in one of your monobehaviours, and call DontDestroyOnLoad on the gameObject. you will also need to use the code below in Awake on your DontDestroyOnLoad() object to make sure only one instance of the MonoBehaviour is available, so you dont create duplicates when you navigate between scenes

 public DataHolder dataHolder;
 public static MyComponent myComponent;
 private void Awake()
 {
   DontDestroyOnLoad(gameObject);
   if (myComponent == null) myComponent = this;
   else Destroy(gameObject);
   
   dataHolder = new DataHolder();

 }

now you can write to this dataHolder object and keep the GameObject alive in case you want to add any more data to it. It is good practice to have a persistant GameObject in the scene to save in-game data, or just about anything that needs to be preserved between scenes.

Alternatively, you can also make your DataHolder class static and it's members static. although i would advise against this, especially if there are multiple players/characters in your game that use the same component. But this will actually help you reference it from any script, without creating objects, or maintaining a persistent GameObject in the scene.

Upvotes: 0

hk1ll3r
hk1ll3r

Reputation: 911

You are allowed to use your own classes / structs in Unity. Unlike Unity components like ScriptableObject and MonoBehaviour, you need to create them and make sure they get deleted.

ScriptableObject public class DataHolder : ScriptableObject { ... } ScriptableObjects are Unity's solution for exactly your use case. You can then create assets that are instances of DataHolder.

In this solution the data gets serialized with the GameObject so you can have default values in Unity Editor Inspector.

Keep using DataHolder as is private void Update () { if (data == null) data = new DataHolder(); ... }

In both these solutions the runtime values will not persist between sessions. To save values and restore them there are different options. Here are a few in order of simplicity:

Upvotes: 1

Related Questions