Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I seem to need to change window to make xml file update

Discussion in 'Editor & General Support' started by Canorange, Mar 26, 2020.

  1. Canorange

    Canorange

    Joined:
    Feb 20, 2020
    Posts:
    4
    I have an inventory saved as an xml file. On start I load it up, then when a item is picked up in the game world the file is updated accordingly. So let's say i have 10 swords, when I pick up another sword it should go up to 11 sword, but some times it doesn't work. I noticed that it works only when I switch out and back in from the Unity editor window. This is the code that updates the xml file:
    Code (CSharp):
    1.     public void addItem(ItemWithAmount it)
    2.     {
    3.         bool isExisting = false;
    4.         foreach(ItemWithAmount current in inv.Items)
    5.         {
    6.             if (it.itemcode == current.itemcode)
    7.             {
    8.                 current.amount++;
    9.                 isExisting = true;
    10.             }
    11.         }
    12.         if (!isExisting)
    13.         {
    14.             inv.Items.Add(it);
    15.         }
    16.  
    17.         XmlSerializer serializer = new XmlSerializer(typeof(Inventory));
    18.         StreamWriter fileWriter = new StreamWriter("Assets/Resources/" + path + ".xml");
    19.  
    20.         var namespaces = new XmlSerializerNamespaces();
    21.  
    22.         serializer.Serialize(fileWriter, inv,namespaces);
    23.         fileWriter.Close();
    24.     }
    What could be causing this problem?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Unity isn't designed for your game to be making changes to files in the Assets folder at runtime. These is because when you make a build none of those files or even folders actually exist anymore.

    The only exception will be a StreamingAssets folder. But the StreamingAssets folder won't actually be in the same location in your build. See the following link on how to use a StreamingAssets folder, which is probably where you want to move your XML file.

    https://docs.unity3d.com/Manual/StreamingAssets.html

    Also, you should be testing your game in a build as much as possible, as editor behavior can be different from build behavior depending on the target platform. If you had tested in a build even once you would have noticed what you're trying to do doesn't work at all, which is why I am pointing this out. It is better to catch these kinds of things earlier, rather than build an entire feature on top of something which simply can't be done the way you're doing it in a build.
     
  3. Canorange

    Canorange

    Joined:
    Feb 20, 2020
    Posts:
    4
    Thanks for your response. I tried using streamingAssets, since I'm building for android I used UnityWebRequest to retrieve the file. I then found out that streamingAssets is read-only in Android. Any other suggestions on how to deal with this?
     
    Last edited: Mar 30, 2020