Author Topic: Text from Resources, single script, multiple scenes  (Read 1856 times)

jcmiguel

  • Guest
Text from Resources, single script, multiple scenes
« on: September 06, 2013, 03:35:36 AM »
I am confused with a NullReferenceException error. I have a script that populates a label from Resource .txt  files. The script runs on different scenes and modifies the text as one chooses a collider. It works flawless the first time you open the scene but if you go back to the main menu and reenters the scene or if you move to a different scene, the error appears. Someone at unity forum suggested that I used Object.DontDestroyOnLoad but seems unlikely to solve my problem

  1.  void ChangeFeaturedText(string filename)
  2.         {
  3.                 //Load text data from resources
  4.                 TextAsset toothdata = (TextAsset)Resources.Load(filename, typeof(TextAsset));
  5.                 reader = new StringReader(toothdata.text);
  6.                
  7.                
  8.                 //confirm if textdata is valid and read it
  9.                 if ( reader == null )
  10.                 {
  11.                 Debug.Log(filename+".txt not found or not readable");
  12.                 }
  13.                 else
  14.                 {
  15.                 // Read each line from the fil+string txt;
  16.                         string txt;
  17.                         longtext="";
  18.             while ( (txt=reader.ReadLine()) != null)
  19.             {
  20.                         longtext += txt;
  21.        
  22.                         }
  23.                         reader.Close();
  24.                         UILabel label = NGUITools.FindInParents<UILabel>(labeltarget);
  25.                         Debug.Log (filename+".txt target found");
  26.                        
  27.                         label.text=longtext;// line where error appears: NullReferenceException: Object reference not set to an instance of an object
  28.                                                                 //SelectFeature.ChangeFeaturedText (System.String filename) (at Assets/Scripts/SelectFeature.cs:106)
  29.                        
  30.                        
  31.                         Resources.UnloadAsset (toothdata);
  32.  
  33.                                
  34.                 }
  35.  
  36.         }

How can I use the same script on different scenes with the ability to move from one to another?
Many thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Text from Resources, single script, multiple scenes
« Reply #1 on: September 07, 2013, 12:44:26 AM »
So the "label" is null. You never check for it being null. I don't know when this function of yours is executed. Apparently it happens before the label it relies upon is set up.

jcmiguel

  • Guest
Re: Text from Resources, single script, multiple scenes
« Reply #2 on: September 12, 2013, 11:47:53 AM »
Solved! I just inserted          
if(label==null)
         {
            Debug.Log ("label is null");
         }
         else...
Thanks