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 /
This question was closed Dec 18, 2017 at 08:56 PM by Tekman03 for the following reason:

Other - I Solved it

avatar image
0
Question by Tekman03 · Dec 18, 2017 at 08:34 PM · errorserializationxmlxmlserializerconstructor

URGENT - Cannot be serialized because it does not have a default public constructor....

Hi there,

I am trying to make a game and need a score system.

The error i get is: InvalidOperationException: ScoreData cannot be serialized because it does not have a default public constructor

And my code is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Xml;
 using System.Xml.Serialization;
 using System.IO;
 using UnityEngine.UI;
 using System;
 
 public class scoresSaver : MonoBehaviour {
 
     public static ScoreList scorelist;
 
     public GameObject HighScoreText;
 
     public Transform parent;
 
     public GameObject text;
 
     public static List<ScoreData> scores = new List<ScoreData>();
 
     void Start()
     {
         LoadScores();
     }
 
     public static void AddScore(string nameIn, int scoreIn)
     {
         XmlSerializer serializer = new XmlSerializer(typeof(ScoreList));
         FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/highscores.xml", FileMode.Open);
         scorelist = serializer.Deserialize(stream) as ScoreList;
         foreach (ScoreData score in scorelist.scores)
         {
             scores.Add(score);
         }
         scores.Add(new ScoreData(nameIn, scoreIn));
     }
 
     public void LoadScores()
     {
         XmlSerializer serializer2 = new XmlSerializer(typeof(ScoreList));
         FileStream stream2 = new FileStream(Application.dataPath + "/StreamingAssets/highscores.xml", FileMode.Open);
         scorelist = serializer2.Deserialize(stream2) as ScoreList;
         foreach (ScoreData score in scorelist.scores)
         {
             scores.Add(score);
         }
         scores.Sort();
         scores.Reverse();
         stream2.Close();
 
         foreach (ScoreData score in scores)
         {
             print(score.Name);
             print(score.Score);
             text = Instantiate(HighScoreText);
             
             text.transform.SetParent(parent);
             text.GetComponent<Text>().text = score.Name + ": " + score.Score;        
         }
     }
 
 }
 
 [System.Serializable]
 public class ScoreData : IComparable<ScoreData>
 {
     public string Name;
     public int Score;
 
     public ScoreData(string newName, int newScore)
     {
         Name = newName;
         Score = newScore;
     }
 
     public int CompareTo(ScoreData other)
     {
         if (this.Score > other.Score)
             return 1;
         else if (this.Score < other.Score)
             return -1;
         else
             return 0;
     }
 }
 
 [System.Serializable]
 public class ScoreList
 {
     public List<ScoreData> scores = new List<ScoreData>();
 }

I hope someone can help.

Many Thanks

Comment
Add comment · Show 2
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 mcolombo · Aug 28, 2019 at 12:22 PM 0
Share

Did you find a solution for this? Im having the same problem, and a parameterless constructor did not fix it.

avatar image Bunny83 mcolombo · Aug 28, 2019 at 05:24 PM 0
Share

That's not possible. This specific error is related to a missing parameterless constructor. So you either have a different error and not the one mentioned here, or you are still missing a public and parameterless constructor for your class. Note that classes without any explicit constructors do automatically get a parameterless constructor. However if you explicitly define a constructor with parameters, the implicit parameterless constructor will not be generated for you. This is different with structs. Structs always have a parameterless constructor and you can not remove it. This is because "default(SomeType)" will actually create an initialized instance of your struct. Initialized means the memory is filled with "0" / null. You also can not explicitly implement a parameterless constructor for structs as this is always implicit. Otherwise when creating an array of a struct type with 1000 elements, the constructor would need to run 1000 times which is not the case. The system just fills the memory with 0. That's also the reason why fields in structs can not have a field initializer. The default value of fields can not be changed, only with a constructor with parameters.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by megabrobro · Dec 18, 2017 at 08:52 PM

You need to set a contructor that accepts no parameters, or if it has parameters they must have default values: https://www.google.co.uk/search?q=what+is+a+default+constructor&ie=utf-8&oe=utf-8&client=firefox-b-ab&gfe_rd=cr&dcr=0&ei=WSo4WsijBsiAgAaYnYX4DA

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

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Xml Serialization of "sub classes" 0 Answers

URGENT - Serializing error The type of argument is not primitive 0 Answers

Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers

DataContract serialization not compatible with Unity? 0 Answers

[SOLVED] XML Deserialization of a single XML file into multiple objects 2 Answers


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