Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by geometa2015 · Oct 09, 2015 at 01:59 PM · serializationsaveloadpersistencebinary

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;
 }




Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FortisVenaliter · Oct 09, 2015 at 02:47 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Serialized data not getting saved in iPhone 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges