Search Unity

Xml not being saved

Discussion in 'Scripting' started by lostudo, Jun 27, 2014.

  1. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    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):
    1.  
    2. void Start ()
    3.     {
    4.         TextAsset textXML = (TextAsset)AssetDatabase.LoadAssetAtPath("Assets/Data/LevelScores.xml", typeof(TextAsset));
    5.         xmlFile = new StringReader(textXML.text);
    6.         filePath = Application.dataPath + "/Data/LevelScores.xml";
    7.         xml = XDocument.Load(xmlFile);
    8.         getLevelScores();
    9.     }
    10. public void getLevelScores()
    11.     {
    12.         MainMenu.Instance.startText.text = xml.Root.Element("Act1").Element("Level1").Element("StarCount").Value;
    13.         a = int.Parse(MainMenu.Instance.startText.text);
    14.         xmlFile.Close();
    15.     }
    16.  
    17.     public void saveLevelScore()
    18.     {
    19.         a++;
    20.         xml.Root.Element("Act1").Element("Level1").Element("StarCount").SetValue(a.ToString());
    21.         writer = new StreamWriter(filePath);
    22.         xml.Save(writer);
    23.         writer.Close();
    24.     }
    25.  
    26. void Update()
    27.     {
    28. if (Input.GetMouseButtonUp(0))
    29.         {
    30.             RaycastHit hit;
    31.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    32.             if (Physics.Raycast(ray, out hit))
    33.             {
    34.                         if (hit.collider.name == "saveScoresButton")
    35.                         {
    36.                             saveLevelScore();
    37.                         }
    38.                         else if (hit.collider.name == "getScoreButton")
    39.                         {
    40.                             getLevelScores();
    41.                         }
    42.              }
    43.          }
    44. }
    45.  
     
    Last edited: Jun 28, 2014
  2. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    You never call saveLevelScore() in the snipped.
     
  3. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    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.
     
  4. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14

    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.
     
  5. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    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.
     
    lostudo likes this.
  6. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
  7. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    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
     
  8. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    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.
     
  9. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    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).
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  11. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    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.
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    No - you load from xmlFile but you save to filePath. You're not using the file you're saving.
     
  13. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    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?