Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sean Catheroy · Apr 14, 2010 at 09:50 PM · nullreferenceexceptiontextasset

TextAsset Giving me NullReference Exception

Ok, this is driving me bonkers. I have searched the forums, but can't find anything that addresses my issue, so here goes.

I'm trying to import a simple text file that describes the height of objects that I want to construct. I had this working completely using monoscript, but since moving to c# I have not been able to import this file, though I have tried implementing several solutions after my own did not work.

The text file holds strings of numbers that I use to determine the scale of Primitives which I am instantiating. My instantiation code works, I just can't load this text file and build using the data it gives me. I have tried placing the asset in various locations, to no avail.

Please let me know what I'm doing wrong. I'm new to c#, so be gentle ;)

using UnityEngine; using System; using System.Collections; using System.IO;

public class LoadLevel : MonoBehaviour {

 //the level as described in a text file   
 public static string[] dataLines;   
 public static int k;

 public static string[] GetLines () {
     ArrayList lines = new ArrayList();
     string line;
     TextAsset textFile = (TextAsset)Resources.Load("text", typeof(TextAsset));
     System.IO.StringReader textStream = new System.IO.StringReader(textFile.text);

     bool match = false;
     while((line = textStream.ReadLine()) != null) {
         lines.Add(line);
     }

     textStream.Close();
     if (lines.Count > 0) {
         return (string[])lines.ToArray(typeof(string));
     }
     return new string[0];
 }

 public static void Start(){
     Debug.Log("LoadLevel Start Got Called!");
     dataLines = GetLines();
     Debug.Log(dataLines.Length);

... ... etc

The error I'm getting is

NullReferenceException: Object reference not set to an instance of an object LoadLevel.GetLines () (at Assets\Standard Assets\Scripts\LoadLevel.cs:20) LoadLevel.Start () (at Assets\Standard Assets\Scripts\LoadLevel.cs:37) Manager.Awake () (at Assets\Standard Assets\Scripts\Manager.cs:30)

I'm hoping this is something simple that I'm just not seeing or that my head is not wrapping properly around.

Any and all help welcome.

Cheers,

S.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sean Catheroy · Apr 14, 2010 at 10:17 PM

Interesting issue here...

The problem is essentially resolved. Walked away, grabbed a coffee, sat back down and realized I had named the asset "text" instead of "test" in the Resource.Load call. So I changed it to the proper name.

However...

When I pressed play in Unity, I got the same error message. Why was that? I decided to quit and restart. I pressed play again: error gone.

Why would the interpreter tell me I had an error that I had resolved?

Side issue, but I hope someone can track that down... it makes debugging a bit of an issue if I have to restart Unity to make sure I've fixed something Unity has told me is broken.

Comment
Add comment · Show 1 · 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 Eric5h5 · Apr 14, 2010 at 11:22 PM 0
Share

Are you sure the error wasn't just left over from the last time? It's not necessary to restart Unity ever that I've encountered.

avatar image
0

Answer by bibo0 · Feb 21, 2017 at 02:08 PM

try to create a new folder and name it Resources and put your xml file in it then try again.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

NullReferenceException when accessing TextAsset from array 1 Answer

BadImageFormatException: Include Script using AssetBundle 1 Answer

Automatic CSV to TXT conversion [solved] 1 Answer

XML File, or Array? 1 Answer

Trouble with Resources.Load 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