Hello everyone, I'm trying to write some data to my xml but it's working in runtime that when i look into my xml it changes.but when i shut the game without looking into my xml it doesn't change. It's like i can't close my xml that it won't save data itself. Anyway below is my code. Code (CSharp): void Start () { TextAsset textXML = (TextAsset)AssetDatabase.LoadAssetAtPath("Assets/Data/LevelScores.xml", typeof(TextAsset)); xmlFile = new StringReader(textXML.text); filePath = Application.dataPath + "/Data/LevelScores.xml"; xml = XDocument.Load(xmlFile); getLevelScores(); } public void getLevelScores() { MainMenu.Instance.startText.text = xml.Root.Element("Act1").Element("Level1").Element("StarCount").Value; a = int.Parse(MainMenu.Instance.startText.text); xmlFile.Close(); } public void saveLevelScore() { a++; xml.Root.Element("Act1").Element("Level1").Element("StarCount").SetValue(a.ToString()); writer = new StreamWriter(filePath); xml.Save(writer); writer.Close(); } void Update() { if (Input.GetMouseButtonUp(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.collider.name == "saveScoresButton") { saveLevelScore(); } else if (hit.collider.name == "getScoreButton") { getLevelScores(); } } } }
What are you actually trying to do here? You're using an API which is only available in the editor. Your code will not work in a standalone, or any build you make. Your code changes an asset that is in the Asset folder. Unity usually looks on the disk for changes. Changes that happen during run-time in the editor might be ignored (I'm not sure). This is the behaviour for pretty much every other change you make during run-time.
I used AssetDatabase cause i read from somewhere that putting xml files to a folder expect from Resources is necessary, but when i put it into the Resources file and use Resources.Load makes no difference. What i really face is i can't save the data to xml automatically. I can change my xml during runtime but when i shut it down and restart it datas in xml returns their previous values.
Correct, you cannot *write* to Resources at runtime. It's an archive file, like a zip file, designed to read assets from. If you want to write to an XML file you'll need to do this using .Net file handling code. This will not work from inside a web player, where you'll want to write the XML to a web server instead.
You maybe can use Unity's Player Prefs( http://docs.unity3d.com/ScriptReference/PlayerPrefs.html ) instead of an XML file. According to your file name you want to store scores in your xml file and that can be done with Unity's Player Prefs very well.
I'm using PlayerPrefs for basic stuffs like activeCharacters or previous level but when it comes to save Level Datas like how many stars player earned for each level and how many quest he accomplished for each level, it's getting complicated for PlayerPrefs and also those values could be hold very systematic in xml. That's why i wanna use xml
So use XML. On web player use an external http server and use WWWPost to send data to it. On other platforms use .Net file handling, as I wrote 5 days ago.
Actually i do change the value in XML at runtime when i call saveLevelScore method and look into my XML file.(Even it is in resources file or another file in assets). But as i'm looking for the solve for a long time, i feel like it's something about refreshing the xml file and make it write the changes itself.(something like StreamWriter.AutoFlash).
You're reading data from a file in your Assets folder (which won't work in a build FYI) but you're saving to some file in the dataPath. You're never actually using the data in the file you save.
Do you mean my filePath is wrong that i call in save method? But when i call save method at runtime and shut the application and than open the xml file the value is changing. Later when i start the project changed value appears. But without looking into my xml file that value doesn't change.
xmlFile I used for reading is loaded from "Assets/Data/LevelScores.xml" and filePath I used to save is Application.dataPath+"/Data/LevelScores.xml", what is the difference?