0

I have made a game with Unity3D that has to access an XML file. I have put the file in the Assets folder and when I debug the game in Unity it works properly. The problem is that when I build the game and launch it in the browser (web player) it doesn't work. It gives the following error:

MethodAccessException: Attempt to access a private/protected method failed

Does it have to do with the security restrictions of the web player?

nix86
  • 2,837
  • 11
  • 36
  • 69
  • 1
    A file being in the Assets folder does not imply it gets included in your build. Did you mean to put it in your Resources? And can you show us the relevant code? – Bart Jul 27 '15 at 15:47
  • In fact I've also tried to put the xml file in the same folder as the executable files, but it doesn't work. As for the code I've used the classes included in System.XML (XMLDocument, XMLNode, XMLNodeList, ...) – nix86 Jul 27 '15 at 15:52
  • How are you accessing the file's contents? WebPlayer tends to restrict access to the file system, which could pose a problem. I've also heard of some rare errors when reflection calls try to access protected constructors. – rutter Jul 28 '15 at 00:32
  • 1
    Try adding the XML file in the Resources folder, and loading it through Resources.Load. Unity includes everything in the Resources folder into your build. Since it's "within" the build, the security sandbox won't throw errors when trying to load the file – Venkat at Axiom Studios Jul 28 '15 at 03:33
  • @Venkat you should provide this as an answer because it is the right way to load external resources in Unity and probably will work even in webplayer. – Frohlich Jul 28 '15 at 17:59
  • @Frohlich, yeah should probably do that. No idea why I stuck it in a comment – Venkat at Axiom Studios Jul 28 '15 at 18:15

2 Answers2

1

Posting comment as an answer here

  1. Move the XML file into the Resources folder.
  2. Use Resources.Load to load the file as a TextAsset
  3. Grab the text using TextAsset.text

Quick Example

public class LoadAnXML : Monobehaviour {

    void Start () {
        var xmlText = Resources.Load<TextAsset>("MyXML").text;
        //Do stuff with the text here
    }
}
Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25
0

Yes, Venkat is right, it's necessary to use Resources.Load. But In my case, as I use C#, the code which works best is this:

XmlDocument xmldocument = new XmlDocument ();
xmldocument.LoadXml (Resources.Load<TextAsset> ("my_xml_file").text);
nix86
  • 2,837
  • 11
  • 36
  • 69