Hello all, i was following brackeys’ tutorial on save systems found here:
and decided to put it into a scene to give it a test run. However attempting to add my script “SaveSystem” to an object gives me the error saying that i cannot because it is abstract. Code below:
using System.IO;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SaveGame (SaveData savedata)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/data.ftyfr";
FileStream stream = new FileStream(path, FileMode.Create);
SaveData gamedata = savedata;
formatter.Serialize(stream, gamedata);
stream.Close();
}
}
You can only attach classes which extend from MonoBehaviour to GameObjects. It’s also declared as static so it can’t be Instantiated in any case. I don’t see any Unity callbacks like Start(), or Update() on this class so what’s the motivation for attaching it to a GameObject?
Nope, that’s a misconception you picked up somewhere. You can use plain old C# objects and classes to your heart’s content. Of course in order to interact with most Unity things, and for the game engine to call your code you will need to have some MonoBehaviours attached to some GameObjects.
Since you have a static method here you can call it from pretty much any context in any of your scripts.
Hi, I know this is an old discussion but I am having a similar issue, all my code is the same and when I drag the script into the button it’s fine except for the fact that the only options in the dropdown are string(). There is no Save() or Load().
That’s a completely different issue, worthy of you starting a completely different thread. In your own thread, show your class code with the code tags.