Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by starikcetin · Jun 07, 2015 at 12:48 PM · c#errorxmlxmlserializernon-static

Need Help With XML Reading Class Initialization

Edit: I hope to end up with something like this:

 int EnemyId = Levels[1].Waves[3].Enemies[5].Id;
 int SpawnAmount = Levels[1].Waves[3].Enemies[5].Amount;

Hi guys, I have an XML scheme like this:

 <WaveList>
     <Level levelId = "1">
         <Wave waveId = "1">
             <Enemy enemyId = "1"> 5 </Enemy>
             <Enemy enemyId = "2"> 0 </Enemy>
             <Enemy enemyId = "3"> 0 </Enemy>
         </Wave>
     </Level>
 </WaveList>

And I have a class scheme like this (in XML reader code):

     [XmlRoot ("WaveList")]
     public class WaveList
     {
         [XmlElement("Level")]
         public List<Level> Levels = new List<Level>();
 
         public class Enemy
         {
             [XmlAttribute ("enemyId")]
             public int Id;
 
             public int Amount;
         }
 
         public class Wave
         {
             [XmlAttribute ("waveId")]
             public int Id;
 
             //[XmlArray ("Wave"), XmlArrayItem("Enemy")]
             [XmlElement("Enemy")]
             public List<Enemy> Enemies = new List<Enemy>();
         }
 
         public class Level
         {
             [XmlAttribute ("levelId")]
             public int Id;
 
             [XmlElement("Wave")]
             public List<Wave> Waves = new List<Wave>();
         }
 }


But when i try to call like this:

         WaveList.Wave waveToSpawn = new WaveList.Wave();
         waveToSpawn = WaveList.Levels[curLevel].Waves[idOfWaveToSpawn];

It gives me a non-static reference error. And i tried with many other combinations and faced different errors. Can anybody give me a tip about how to fetch a wave and enemies in that wave from this code?

If my code is rubbish, you may suggest a new code scheme too, of course.

Thanks in advance!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jun 08, 2015 at 05:22 AM

Well, you haven't included where you actually deserialize your XML file... At some point you have to actually read and deserialize your file and as a result you get an instance of your WaveList class. Since all your fields are instance fields (which they have to be or they can't be serialized) you need an instance of your class.

Your two lines of code both make not much sense:

 WaveList.Wave waveToSpawn = new WaveList.Wave();
 waveToSpawn = WaveList.Levels[curLevel].Waves[idOfWaveToSpawn];

You create a new instance of your "Wave" class and in the next line you want to replace it with an instance of your Waves List. It's pointless to create that instance in the first place.

Now to your actual problem. The "Levels" List (declared in line 5 of your WaveList class) is an instance variable of that class but here: WaveList.Levels you try to use it like a static variable. You have to use your deserialized instance instead of the class name

 WaveList waveToSpawn;
 
 // waveToSpawn has to be initiallized / deserialized before you can use it below
 
 waveToSpawn = waveListInstance.Levels[curLevel].Waves[idOfWaveToSpawn];
Comment
Add comment · Show 3 · 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
avatar image starikcetin · Jun 08, 2015 at 02:05 PM 0
Share

Thank you for your clear explanation but, i actually don't know how to deseralize and initialize an xml file. Can you give an example or simply forward me to some articles about them?

avatar image Bunny83 · Jun 08, 2015 at 03:55 PM 1
Share

Uhm, from the way the question is written i thought you already using something like the XmlSerializer class. You talked about your "X$$anonymous$$L reader code" in your question. Where do you read the X$$anonymous$$L?

You might want to have a look at this $$anonymous$$SDN page about X$$anonymous$$L serialization a few pages down there are examples with custom classes.

Your probably need to put the XmlTextAttribute on your Amount field. Otherwise the way you have your classes setup at the moment the serializer expects an Enemy to look like this:

 <Enemy enemyId = "1"> <Amount> 5 </Amount> </Enemy>

So your Enemy should look like this:

 public class Enemy
 {
     [XmlAttribute ("enemyId")]
     public int Id;
     
     [XmlText]
     public int Amount;
 }

See this SO question about the XmlTextAttribute.

avatar image starikcetin · Jun 08, 2015 at 04:38 PM 1
Share

I solved it. Thank you man!

By the way, for those need a solution out there, here is the function i used to read and deserialize X$$anonymous$$L:

         public static WaveList Load(string path)
         {
             var serializer = new XmlSerializer(typeof(WaveList));
             using(var stream = new FileStream(path, File$$anonymous$$ode.Open))
             {
                 return serializer.Deserialize(stream) as WaveList;
             }
         }

Usage:

         WaveList _WaveList;
         string fullPath = Path.Combine(Application.dataPath, "X$$anonymous$$L/WaveList.xml");
         _WaveList = WaveList.Load(fullPath);



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

21 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

Related Questions

Multiple Cars not working 1 Answer

Loading Xml file issues 0 Answers

xml parsing error 0 Answers

Xml Serialization? (how to read/write?) 1 Answer

How do I make this XML file parser work in Unity 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