0

I'm new to programming and I have this script that I'm making, it makes a function that reads XML files based on your input, I've just ran into this issue though where I cannot access the variable named "XMLtext", it's public, can someone tell me what I'm doing wrong and explain it, I've tried various things.

using UnityEngine;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Collections;

public class Data : MonoBehaviour {
    public TextAsset XMLtext;

    void Main () {

    }

    public static string XMLread (params string[] no) {
        var XMLfile = XDocument.Parse ();
        var a = Data.XMLfile.Element ("data");

        for (int i = 0; no[i] == null; i++) {

        }
    }
}
user2690614
  • 11
  • 1
  • 4
  • I'm sorry, I should have mentioned that the XMLtext variable will be in "var XMLfile = XDocument.Parse (XMLtext.text); – user2690614 Aug 16 '13 at 20:09
  • Isn't XMLText a reserved word of the unity framework? just guessing.. – CaveCoder Aug 16 '13 at 20:12
  • Well, I feel like an idiot now, I managed to solve by problem by using "static" in front of the declaration, even though I tried that before and it didn't work. Oh well, could someone be kind enough to explain why it needs the static prefix to be accessible though? – user2690614 Aug 16 '13 at 20:13
  • 2
    Thas because you are calling the variable inside a static method, static method and variables are shared with all instances of an object..http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.90).aspx – CaveCoder Aug 16 '13 at 20:17
  • Thanks, and I don't think it's reserved, I've done other things using that name with no issues. – user2690614 Aug 16 '13 at 20:19

2 Answers2

0

The problem is that your class Data, which owns the XMLtext field is an instance class. Your method XMLread is static, meaning it is shared across all instances of Data. To solve your problem either make XMLText static or make XMlread an instance method by removing the static keyword.

chris.house.00
  • 3,273
  • 1
  • 27
  • 36
0

The problem is that your XMLtext field belongs to an instance of Data, while your XMLread method is static, so it belongs to the class Data. You probably just want to remove the static from XMLread, so that you're always in an instance of Data.

Tim S.
  • 55,448
  • 7
  • 96
  • 122