0
votes

I wanna ask something. I am making a game, and i am trying to make saving and loading the data of the game just like harvest moon, but i got this error. I have searched this kind of error in google, but i have no idea how to solve it. And this is my code.

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO;

public class gameController : MonoBehaviour {
    public static gameController control;
    public basicskill bs;

    // Use this for initialization
    void Awake () {
        if(control == null)
        {
            DontDestroyOnLoad(gameObject);
            control = this;
        }
        else if(control != this)
        {
            Destroy(gameObject);
        }

    }

    void OnGUI(){
        GUI.Label (new Rect (10, 10, 100, 30), "Health: " + bs.charPrince.health);
        GUI.Label (new Rect (10, 40, 140, 30), "Experience: " + bs.charPrince.exp);
    }

    public void Save(){

        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");

        Prince data = new Prince ();
        data.name = bs.charPrince.name;
        data.birthday = bs.charPrince.birthday;
        data.hadapAtas = bs.charPrince.hadapAtas;
        data.hadapKanan = bs.charPrince.hadapKanan;
        data.locationY = bs.charPrince.locationY;
        data.locationX = bs.charPrince.locationX;
        data.exp = bs.charPrince.exp;
        data.health = bs.charPrince.health;
        bf.Serialize (file, data);
        file.Close ();
    }

    public void Load(){
        if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            Prince data = (Prince)bf.Deserialize(file);
            file.Close();

            bs.charPrince.name = data.name;
            bs.charPrince.birthday = data.birthday ;
            bs.charPrince.hadapAtas = data.hadapAtas ;
            bs.charPrince.hadapKanan = data.hadapKanan;
            bs.charPrince.locationY = data.locationY;
            bs.charPrince.locationX = data.locationX;
            bs.charPrince.exp = data.exp;
            bs.charPrince.health = data.health;
        }
    }

    // Update is called once per frame
    void Update () {

    }
}

From code above, i got error

SerializationException: Type UnityEngine.GameObject is not marked as Serializable. System.Runtime.Serialization.Formatters.Binary.BinaryCommon.CheckSerializable (System.Type type, ISurrogateSelector selector, StreamingContext context) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryCommon.cs:119) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteValue (System.IO.BinaryWriter writer, System.Type valueType, System.Object val) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:732) Prince__TypeMetadata.WriteObjectData (System.Runtime.Serialization.Formatters.Binary.ObjectWriter , System.IO.BinaryWriter , System.Object ) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:360) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:293) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:271) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:256) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:232) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:211) gameController.Save () (at Assets/Scripts/gameController.cs:45) adjustScript.OnGUI () (at Assets/Scripts/adjustScript.cs:32)

and this is my other codes

using UnityEngine;
using System.Collections;
using System;

public class adjustScript : MonoBehaviour {
    void OnGUI(){
        if(GUI.Button(new Rect(10,100,100,30),"Health Up"))
        {
            gameController.control.bs.charPrince.health += 10;
        }
        if(GUI.Button(new Rect(10,140,100,30),"Health Down"))
        {
            if(gameController.control.bs.charPrince.health >0)
            {
                gameController.control.bs.charPrince.health -= 10;
            }
        }
        if(GUI.Button(new Rect(10,180,100,30),"Experience Up"))
        {
            gameController.control.bs.charPrince.exp += 10;

        }
        if(GUI.Button(new Rect(10,220,100,30),"Experience Down"))
        {
            if(gameController.control.bs.charPrince.exp >0)
            {
                gameController.control.bs.charPrince.exp -= 10;
            }
        }
        if(GUI.Button(new Rect(10,260,100,30),"Save"))
        {
            gameController.control.Save();

        }
        if(GUI.Button(new Rect(10,300,100,30),"Load"))
        {
            gameController.control.Load();

        }
    }
}



using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

[Serializable]
public class Prince : Character{
    public float health = 100f;
    public float exp = 0f;
    public float kecMaks = 0f;
    public float moveV = 0f;
    public float moveH = 0f;
    public bool hadapKanan = false;
    public bool hadapAtas = true;
    public GameObject objPrince;

    public Prince()
    {
        this.name = "";
        this.birthday = "";
        this.objPrince = null;
    }
    public Prince(GameObject objChar,string nama, string birthday){
        this.name = nama;
        this.birthday = birthday;
        this.objPrince = objChar;
    }
}


using UnityEngine;
using System.Collections;
using System;

[Serializable]
public class Character  {
    public string name;
    public string birthday;
    public float locationX;
    public float locationY;

    public Character(){
        this.name = "";
        this.birthday = "";
        this.locationX = 0f;
        this.locationY = 0f;
    }
}

Please help me to solve this problem. I learned it from unity's live tutorial about persistence- saving and loading data, but the one who made this kind of code didn't have this error.

1
Error message describes what is wrong. Can you mark GameObject as Serializable? Did tutorial really provided that Prince class must have GameObject property?Renatas M.
this is your reference to the current instance of Prince - you don't need a GameObject objPrince do you? The Prince class is marked [Serializable] but it can't serialize the GameObject so you either need to remove it or make it serializableuser2140173

1 Answers

0
votes

I bummed into the same error... found this as a solution for me: (in your case)

[System.NonSerialized]
private GameObject objPrince;

public GameObject ObjPrince{get{return objPrince;}set{objPrince = value;}}

This does mean you can't save (serialize) the objPrince, but you can have a live reference during runtime. To get the needed prefab for objPrince I load it from Resources by "namePrince" each time i Instantiate.

also there mostly isn't really a reason a GameObject should be saved, unless a player can edit the GameObject during game time or something like that. and even then you could still save out the names from the used "body parts" or whatever and load the from the Resources folder when needed.

hope this still helps.