Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resources.Load in static class

Discussion in 'Scripting' started by Siames, Dec 7, 2016.

  1. Siames

    Siames

    Joined:
    Dec 1, 2016
    Posts:
    15
    i'm trying to load a XML

    Code (CSharp):
    1.  
    2. public static class selIdioma
    3. {
    4. static XmlDocument xDoc;
    5. public static void cargarIdioma()
    6. {
    7. string name=preferencias.pref.idioma;
    8. TextAsset textAsset=(TextAsset)Resources.Load(name);
    9. xDoc.LoadXml(textAsset.text);
    10. }
    11. }
    Name without extension. I've folder Resources into Assets. But it shows me this message
    Why?
    (I'm sorry for my english)
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Which reference is null?
     
  3. Siames

    Siames

    Joined:
    Dec 1, 2016
    Posts:
    15
    Line 9, although when arrives textAsset is null
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    According to the docs this will occur if the asset is not found at the specified path. Check the path is correct.
     
  5. Siames

    Siames

    Joined:
    Dec 1, 2016
    Posts:
    15
    I have tried
    Code (CSharp):
    1.  
    2. TextAsset textAsset = (TextAsset) Resources.Load(Application.dataPath +"/Resources/"+name);
    3. TextAsset textAsset = (TextAsset) Resources.Load(name);
    4. TextAsset textAsset = (TextAsset) Resources.Load("/Resources/"+name);
    5. TextAsset textAsset = (TextAsset) Resources.Load("Resources/"+name);
    Name with extension and without extension.
    I do not know if i need to reference assets folder
    The path is Assets/Resources/archivo.xml
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The path is relative to the resources folder. So there is no need to include Resources. Just the name should be enough. Assuming you have the correct name?
     
  7. Siames

    Siames

    Joined:
    Dec 1, 2016
    Posts:
    15
    Yes, the name is correct, i rename it to 1.xml and 0.xml.
    Could be for the static class ?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No. Static has nothing to do with this.

    Resources.Load is simply not finding an asset in the Resources folder with the appropriate name.
     
  9. Siames

    Siames

    Joined:
    Dec 1, 2016
    Posts:
    15
    it should find them

     
  10. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'd be guessing that static XmlDocument xDoc; is null, it's never been assigned.

    Change line 4 to:

    static XmlDocument xDoc = new XmlDocument();
     
    Siames, Ryiah and Kiwasi like this.
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This.

    Teach me for believing the OP. ;)
     
    larku likes this.
  12. Siames

    Siames

    Joined:
    Dec 1, 2016
    Posts:
    15
    It worked, thank you very much.
     
    larku likes this.