Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jcmiguel · Sep 05, 2013 at 11:42 AM · resources.loadtxtunloading

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fherbst · Sep 05, 2013 at 11:43 AM 0
Share

What's at line 106?

avatar image jcmiguel · Sep 05, 2013 at 11:48 AM 0
Share

label.text=longtext;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jcmiguel · Sep 06, 2013 at 08:37 AM 0
Share

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?

avatar image DuckOfDoom · Sep 06, 2013 at 08:49 AM 0
Share

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.

avatar image jcmiguel · Sep 06, 2013 at 09:01 AM 0
Share

$$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.

avatar image DuckOfDoom · Sep 06, 2013 at 09:36 AM 0
Share

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.

avatar image jcmiguel · Sep 09, 2013 at 08:33 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Font 1 Answer

Set text to line in .txt file depending on player skill level 1 Answer

Best approach for 2.5D scene tiles 2 Answers

Get file in project using it's path 1 Answer

when loading a textasset from .txt file, the quotations disappear?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges