- Home /
Save System Problems
I wanted to have a save system and did everything like in the video:" SAVE & LOAD SYSTEM in Unity " from Brackeys But i get the error message in Unity : SerializationException: Type 'UnityEngine.Transform' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at :0)
Please Help :(
SaveSystem:
using System.IO; using UnityEngine; using System.Runtime.Serialization.Formatters.Binary; using System;
public static class SaveSytem { public static void SavePlayer (Spieler player) { BinaryFormatter formatter = new BinaryFormatter(); string path = Application.persistentDataPath + "/player.fun"; FileStream stream = new FileStream(path, File$$anonymous$$ode.Create);
SpielerDaten data = new SpielerDaten (player);
formatter.Serialize(stream, data);
stream.Close();
}
public static SpielerDaten LoadPlayer()
{
string path = Application.persistentDataPath + "/player.fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream= new FileStream(path, File$$anonymous$$ode.Open);
SpielerDaten data = formatter.Deserialize(stream) as SpielerDaten;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
SpielerDaten:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable] public class SpielerDaten { private Transform playerTransform;
public float[] position;
private Spieler player;
public SpielerDaten (Spieler player)
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
position = new float[3];
position[0] = playerTransform.position.x;
position[1] = playerTransform.position.y;
position[2] = playerTransform.position.z;
}
}
Spieler:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Spieler : $$anonymous$$onoBehaviour { public void SavePlayer() { SaveSytem.SavePlayer(this); }
public void LoadPlayer()
{
SpielerDaten data = SaveSytem.LoadPlayer();
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
}
}
A Transform is not a serializable class as the error message shows. So you can't use it for that purpose. You can check out this reddit post about the same question, where there is also a solution posted for this problem. https://www.reddit.com/r/Unity3D/comments/6h7t4x/question_on_serializing_a_list_of_transforms/
If you look at the SpielerDaten script, you do it right for the position. You save the position in a float[] (which could probably just be a Vector3). This will work because that is a serializable type. In the same script you try to serialize a transform which is something you cannot do. You have to serialize the components of the transform: Position, Rotation and scale as Vector3's (or float[] if that is what you need or prefer). Then later when you need the data you can reconstruct the transform, using the Vector3's.
So it would look something like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable] public class SpielerDaten {
public Vector3 position;
public Vector3 eulers;
public Vector3 scale;
private Spieler player;
public SpielerDaten (Spieler player)
{
Transform playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
position = playerTransform.position;
eulers = playerTransform.eulerAngles;
scale = playerTransform.localScale;
}
Your answer
Follow this Question
Related Questions
Save/load playerprefs 2 Answers
Saving/Load using menu C# 1 Answer
Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers
Strategy for saving game 1 Answer