Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Risal Fajar · Aug 11, 2015 at 02:01 AM · c#androidsaveloadxml

Save and Load data from Local XML file won't work

I'm building my first prototype for multi-game sharing achievement system. It means there's two or more games using one shared achievement data. I'm storing the data in an .xml file, for now the xml only contains coins data.

So i want my program to load and modify the amount of coins from it.

This is My Xml :

 <Achievement><Coins>800</Coins></Achievement>

My Code :

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Xml;
 using System.Xml.Serialization;
 using System.IO;
 
 public class XMLTest : MonoBehaviour {
 
 string Filelocation, FileName;
 
 private Text CoinsText;
 
 public int Coins;
 public int Price;
 void Start () {
     CoinsText = GameObject.Find("CoinsText").GetComponent<Text>();
     Filelocation = "E:/Projek/Projek Pak Suprapedi/XML Test/";
     FileName = "Coins.xml";
 }
 
 public void WriteToXML()
 {
     XmlDocument XmlDoc = new XmlDocument();
     if(File.Exists(Filelocation + FileName))
     {
         XmlDoc.Load(Path.Combine(Filelocation, FileName));
         XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
         foreach(XmlNode node in AchievementList)
         {
             if(node.Name == "Coins")
             {
                 node.InnerText = Coins.ToString();
             }
         }
         XmlDoc.Save(Path.Combine(Filelocation, FileName));
     }
 }
 
 public void LoadFromXML()
 {
     XmlDocument XmlDoc = new XmlDocument();
     if(File.Exists(Filelocation + FileName))
     {
         XmlDoc.Load(Path.Combine(Filelocation,FileName));
         XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");
         foreach(XmlNode node in AchievementList)
         {
             if(node.Name == "Coins")
             {
                 Coins = int.Parse(node.InnerText);
             }
         }
     }
 }
 
 public void Buy()
 {
     Coins -= Price;
 }
 
 public void GetMoney()
 {
     Coins += Price;
 }
 
 void Update()
 {
         CoinsText.text = "Your Coins : " + Coins.ToString();
     }
 }

I'm assigning the public void to a button so i can modify the coins then save or load it. But it seems that load and save function won't do anything, nothing happen to xml file.

I will deploy my game to Android platform, but I don't know if this method would work, would it ?

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 kmgr · Aug 11, 2015 at 07:21 AM 0
Share

In the code you provided there is no call to either WriteToX$$anonymous$$L() or LoadFromX$$anonymous$$L(). Show us how you call these methods.

avatar image Risal Fajar · Aug 11, 2015 at 11:15 AM 0
Share

@kmgr I'm using button for each public function, there's 4 button, one for save, load, decrease, and increase money

1 Reply

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

Answer by Risal Fajar · Aug 11, 2015 at 09:25 PM

I will answer my own question. I've found the problem. so the problem is :

 XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Achievement");

I'm replacing it with

 XmlNodeList AchievementList = XmlDoc.GetElementsByTagName("Coins");

And it works !!

I think you can understand what is the problem now

Comment
Add comment · Show 1 · 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 onrakc · Feb 11, 2016 at 12:05 AM 0
Share

@Risal Fajar i tried your solution for xml and it works perfectly on editor and pc (thank you by the way). but couldn't find a way to work on Android. $$anonymous$$ay you tell me how can it work on $$anonymous$$obile too (android and ios)

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

SaveLoad Profile c# to xml 4 Answers

Save and Load from XML help 1 Answer

Loading Xml file issues 0 Answers

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

Filestream/SaveManager 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