- Home /
Loading TextAsset under different scenes
I am using TextAsset to load .txt resources into a label:
//Load text data from resources
TextAsset toothdata = (TextAsset)Resources.Load(filename, typeof(TextAsset));
reader = new StringReader(toothdata.text);
//confirm if textdata is valid and read it
if ( reader == null )
{
Debug.Log(filename+".txt not found or not readable");
}
else
{
// Read each line from the fil+string txt;
string txt;
longtext="";
while ( (txt=reader.ReadLine()) != null)
{
longtext += txt;
}
reader.Close();
UILabel label = NGUITools.FindInParents<UILabel>(labeltarget);
Debug.Log (filename+".txt target found");
//the problem is below
label.text=longtext;// line 106
//the problem is above
Resources.UnloadAsset (toothdata);
This script is shared among different scenes. If I open a single scene it works flawless but if I move from one scene to another the script gives me the following: NullReferenceException: Object reference not set to an instance of an object and points to line 106 (or line 25 in the script above) on that script. The problem persists even if I request text on one scene, return to the main and go back to that scene again. I am pretty sure I need to unload something but I am stuck. Any help? Thanks
Answer by DuckOfDoom · Sep 05, 2013 at 01:35 PM
UILabel label = NGUITools.FindInParents<UILabel>(labeltarget);
The problem might be here. There was a "labeltarget" object in the first scene but in the second it got destroyed. If you use "DontDestroyOnLoad" on object with this script, you will have the object, but all the references within it will be dead because when scene switched, all referenced objects got destroyed.
Therefore "NGUITools.FindInParents" couldn't find the label and therefor adressing label.text gives you the exception.
Sorry but I am confused now. What should I do to allow the same script to run into different scenes without giving me the error?
You should try to either create a new instance of this object(no idea why you need the same one across all the scenes) or you have to find new "labeltarget" each time you load a new scene.
Good thing is, if your GUI spreads across multiple scenes, to save it into prefab and have all scripts inside reference the objects in that prefab.
$$anonymous$$aybe I didn't manage to explain this script clearly. Each scene has its own label and labeltarget is exposed with: public GameObject labeltarget; on the script. I would expect this to be destroyed as you move from one scene to another and then be recreated as you open the scene.
It is recreated, but "public GameObject labeltarget" holds reference to the labeltarget from the previous scene(which was destroyed), and thats why there is a NullReferenceException. As I said, you have to either assing new labeltarget manually, or store it in prefab and have the script reference it in prefab.
This is driving me mad. I followed your suggestion using a prefab but that did not solve the problem. I have a main scene that does not have any label but links to subscenes; from that you can move to other scenes where you browse a 3d object that has features (colliders), which are explained on the label. It is an anatomy "serious game" and if I could reuse the script on different scenes as well as the label that will facilitate the development. The label contents are kept on resources to facilitate correction and, later on, localisation. Any alternative approach will be appreciated.