• 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
Question by rainbowparadoxes · Apr 15, 2017 at 08:13 AM · serializationobjectsxmlclassesxmlserializer

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

Hi guys,

I've been using Unity for a while now, but I'm only recently digging into XML files in order to save a list of objects. I'm following this tutorial:

http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer

And I followed it to a T, but when I attempt to write to a file I get the following error: Assets/Scripts/ChickenClass.js(23,49): BCE0018: The name 'ChickenCollection' does not denote a valid type ('not found').

All I want to do is save an object and its paramaters as an element of a list of objects in a file, then read it later.

Here's my code:

 import System.Collections.Generic;
 import System.Xml.Serialization;
 import System.Xml;
 import System.IO;
  
  @XmlRoot("ChickenCollection")
 public class ChickenContainer
  {
      @XmlArray("Chickens")
      @XmlArrayItem("Chicken")
      public var Chickens : List.<Chicken>;
  
      public function Save(path : String)
      {
          var serializer : XmlSerializer = new XmlSerializer(ChickenContainer);
          var stream : Stream = new FileStream(path, FileMode.Create);
          serializer.Serialize(stream, this);
          stream.Close();
      }
  
      public static function Load(path : String):ChickenContainer 
      {
          var serializer : XmlSerializer = new XmlSerializer(ChickenContainer);
          var stream : Stream = new FileStream(path, FileMode.Open);
          var result : ChickenContainer = serializer.Deserialize(stream) as ChickenContainer;
          stream.Close();
          return result;
      }
  
          //Loads the xml directly from the given string. Useful in combination with www.text.
          public static function LoadFromText(text : String):ChickenContainer
          {
          var serializer : XmlSerializer = new XmlSerializer(ChickenContainer);
          return serializer.Deserialize(new StringReader(text)) as ChickenContainer;
      }
  }
 

And in my class file:

 class ChickenClass extends MonoBehaviour{
     public class Chicken{
         var name : String;
         var age : int; // 0 is egg, 1 is chick, 2 is hen
         var ageSeconds : float;
 
         // Aesthetic Features
         var featherColor : String;
         var beakColor : String;
         var combColor : String;
         var price : int;
         var hunger : int;
         var box : int;
 
         private function Chicken(){
          // Empty Constructor for serialization
         }
 
         public function Save(){
             // Write self to XML file
             var chickenCollection : ChickenCollection = ChickenContianer.Load(Path.Combine(Application.dataPath, "chickens.xml"));
             chickenCollection.Save(Path.Combine(Application.persistentDataPath, "chickens.xml"));
         }
 
         public function Chicken(chickenName : String, ageState : int, ageSecs : float, color0 : String, color1 : String, color2 : String, priceOfChicken : int, hungerNum : int, boxNum : int){
             name = chickenName;
             age = ageState;
             ageSeconds = ageSecs;
             featherColor = color0;
             beakColor = color1;
             combColor = color2;
             hunger = hungerNum;
             box = boxNum;
             price = priceOfChicken;
         }
     }
 }

Could you please point me in the right direction? I've looked through a lot of docs and I'm not sure where to go from here. My game is nearly functional, but it won't work without this saving feature.

Thank you so much.

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Xml Serialization of "sub classes" 0 Answers

DataContract serialization not compatible with Unity? 0 Answers

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

ArgumentException: Path is empty while saving data in an XML 0 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