0
\$\begingroup\$

I got 2 big problems that after a lot of research I´m unable to solve.

First problem: I got 5 xml files and a sub-folder inside the Resources folder but when I build my project, the Resources folder is empty (well, only two unity files in there: unity default resources and unity_builtin_extra. So I can´t access my xml files when I do: Resources.Load, anywhere on my code.

I post images to clarify: enter image description here

Second problem: As Resources file was not working I decided to use public variables for my xml files declaring: public TextAsset myXmlDoc, but I realize that, only when I build this isn´t working as it gets NullReferenceException as if my public TextAsset variable wasn't assigned by me (wich is not true)

I post images and code to clarify:

public class LoadXml_Narrator : MonoBehaviour {

    NarratorTextClass narratorClass;
    public TextAsset file;

    void Awake()
    {

        print("Xml file " + file);
    }
}

enter image description here

This is the output log of my Build:

NullReferenceException: Object reference not set to an instance of an object at LoadXml_Narrator.Awake () [0x0001d] in D:\Projects\PC\Majorel_\Majorel_\Assets\Scripts\XML\Narrator\LoadXml_Narrator.cs:20

(Filename: D:/Projects/PC/Majorel_/Majorel_/Assets/Scripts/XML/Narrator/LoadXml_Narrator.cs Line: 20)

Note: IN the editor everything works perfect. The issue is only when build and execute the project.

\$\endgroup\$
  • \$\begingroup\$ It's expected that your built game's folder structure will look different from your source files. One of the engine's jobs is to package up the files into pre-digested formats that are fast for the game to load. So there's nothing wrong with loading a file via Resources.Load or a reference to a TextAsset. Your problem might lie elsewhere, like in file access permissions that are preventing Unity from packaging up the file, or a scene setup/script error that leads to your variable not being populated the way you expect at runtime. Can you try reproducing the problem in a new project? \$\endgroup\$ – DMGregory Jul 19 '18 at 13:08
  • \$\begingroup\$ @DMGregory Thanx for your reply. I understand Unity compress files so it's easier to load, but then, how I could read a xml file from Resources.Load if after the build it isn't a xml anymore? I opened a new project and the Resources folder issue still happens but not the public variable one (in a new project this works great). In the original project I tried with public variables in different scripts and different scenes and happens all the time. I really don't know what it can be, if there's an option or something like that I'm totally lost with that. \$\endgroup\$ – Bund187 Jul 19 '18 at 15:02
  • 1
    \$\begingroup\$ It sounds like you're using Resources.Load incorrectly, but without seeing your code it's hard to know for sure. One common error is including the path to the resources folder or the file extension. If the public variable error isn't reproducible in a new project, then there's some error in your original project interfering with it. Try making a copy of your original project and strip out complexity a little at a time to isolate what's causing your public variable to end up un-set. Common errors here include unwanted stray copies of objects in the scene, deleted objects, and execution ordering \$\endgroup\$ – DMGregory Jul 19 '18 at 15:19
  • \$\begingroup\$ I found the origin of the problem. I had marked the checkbox: Scripts Only Build at Build Settings. Honestly, I don't know why becasue I don't even know what it does. Anyway thank you so much for pointing me in the right direction. I Was about to go crazy. Thank you so much!! @DMGregory \$\endgroup\$ – Bund187 Jul 19 '18 at 16:34
  • \$\begingroup\$ I recommend adding that as an answer so it's easy for other users with similar issues to find the solution. Including a bit of research into what that option does would probably help them too. :) \$\endgroup\$ – DMGregory Jul 19 '18 at 16:36
1
\$\begingroup\$

I found the origin of the problem. I had marked the checkbox: Scripts Only Build at Build Settings. Honestly, I don't know why becasue I didn't even know what it does. Anyway thank you so much for pointing me in the right direction. I Was about to go crazy.

After some research I found little info about this option, even the manual doesn´t show it https://docs.unity3d.com/Manual/BuildSettings.html The documentation just say: Build only the scripts of a project. So I guess it includes only some of the scripts for speed reasons when building.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.