- Home /
SerializationException: Type UnityEngine.UI.Button is not marked as Serializable. Solution
Hi, Im a beginner to Unity3d and C#, I'm inserting the cube and sphere when the toggle is on and i want to save and load the scene. hence , i tried the following code and i got a serialization exception.I'm unable to fix the serialization exception, could someone please help. I'm trying to save the scene to the file and load from the file. I'm attaching the following code below, please help.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class spawn : MonoBehaviour
{
public static spawn control;
public GameObject user1_cube;
public Button Savebtn;
public Button Loadbtn;
public Button cube_insert;
public Toggle cubeToggle, sphereToggle;
public RaycastHit hit;
void Awake()
{
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if (control != this)
{
Destroy(gameObject);
}
}
void Start()
{
demo();
}
void demo()
{
cubeToggle = GameObject.FindGameObjectWithTag("cubeToggle").GetComponent<Toggle>();
sphereToggle = GameObject.FindGameObjectWithTag("sphereToggle").GetComponent<Toggle>();
Savebtn = GameObject.FindGameObjectWithTag("Savebtn").GetComponent<Button>();
Loadbtn = GameObject.FindGameObjectWithTag("Loadbtn").GetComponent<Button>();
}
void Update()
{
if (cubeToggle.isOn)
{
if (Input.GetButtonDown("Fire1"))
{
var mousep = Input.mousePosition;
mousep.z = 4.0f;
var objectpos = Camera.main.ScreenToWorldPoint(mousep);
GameObject SpawnLocation;
SpawnLocation = (GameObject)Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), objectpos, Quaternion.identity);
}
}
if (sphereToggle.isOn)
{
if (Input.GetButtonDown("Fire1"))
{
var mousep = Input.mousePosition;
mousep.z = 4.0f;
var objectpos = Camera.main.ScreenToWorldPoint(mousep);
GameObject SpawnLocation;
SpawnLocation = (GameObject)Instantiate(GameObject.CreatePrimitive(PrimitiveType.Sphere), objectpos, Quaternion.identity);
}
}
Savebtn.onClick.AddListener(delegate { Save(); });
Loadbtn.onClick.AddListener(delegate { Load(); });
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.cube_insert = cube_insert;
data.cubeToggle = cubeToggle;
data.sphereToggle = sphereToggle;
data.Savebtn = Savebtn;
data.Loadbtn = Loadbtn;
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);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
data.cube_insert = cube_insert;
data.cubeToggle = cubeToggle;
data.sphereToggle = sphereToggle;
data.Savebtn = Savebtn;
data.Loadbtn = Loadbtn;
}
}
}
[Serializable]
class PlayerData
{
public Button cube_insert;
public Toggle cubeToggle, sphereToggle;
public Button Savebtn;
public Button Loadbtn;
}
Format your code properly and include the text from the exception if you want a good answer. It's really hard to read as-is.
Answer by Bunny83 · Oct 09, 2015 at 02:59 PM
You can't serialize classes derived from UnityEngine.Object at all. So you can't serialize Button, or Toggle references.
I don't even see the point of serializing references to certain UI elements in a certain scene.
Your answer
Follow this Question
Related Questions
Delete a serialized binary save file 2 Answers
Serializer deletes saved info on game restart 1 Answer
Unity Not Loading 0 Answers
Unity Serializer scripting error 1 Answer