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 Christopher K. · Oct 20, 2013 at 06:42 AM · nullreferenceexceptiontextasset

NullReferenceException when accessing TextAsset from array

Hey,

I'm experiencing a NullReferenceException whenever I try to access a TextAsset from an array that I've defined in the Unity Editor.

 var partyfiles : TextAsset[];
 var party = new Array();
 
 function Awake() {
 instance = this;
 DontDestroyOnLoad(gameObject);
 }
 
 function Start() {
 InitializeHeroes();
 }
 
 //Function to load party from text files
 function InitializeHeroes(){
     var charReader : CharacterReader;
     party = new Hero[partyfiles.length];
     for(var i=0; i<partyfiles.length; i++){
         party.push(charReader.BuildCharacter(partyfiles[i]));
     }
 }

In the editor, I've attached the script to an empty game object and set the array to length 2 with text assets in each member of the array, but the game then stops and says that a NullReferenceException has occurred. Any thoughts on why this would be and how I'd go about amending it?

Comment
Add comment · Show 3
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 rutter · Oct 20, 2013 at 06:56 AM 0
Share

Which line is throwing the exception? If this script is attached to multiple GameObjects, is the array populated on all of them?

avatar image Christopher K. · Oct 20, 2013 at 07:35 AM 0
Share

The line throwing the exception is the party.push at the end.

avatar image Christopher K. · Nov 06, 2013 at 10:34 PM 0
Share

Update: It's not the textasset which is causing the problem, it's the CharacterReader. I think I need to add a CharacterReader component to the gameobject in question.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Starwalker · Nov 06, 2013 at 10:56 PM

Your doing multi-array operations on the same line, suggest separating the operation in order to find out which array is the problem.

In the above code: var charReader : CharacterReader; -> .Buildcharacter if blank will cause the exception

partyfiles.length , if 0, will create a party[] of length 1, for loop & will quit on 0th iteration, i.e for(var i=0; i

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 Christopher K. · Nov 06, 2013 at 11:09 PM 1
Share

var charReader : CharacterReader; -> .Buildcharacter if blank caused the exception, it seems. I've stripped away the array operations for the time being while working on the problem, and am now debugging CharacterReader, so at least it's a step in the right direction. Thanks!

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

17 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

Related Questions

TextAsset Giving me NullReference Exception 2 Answers

BadImageFormatException: Include Script using AssetBundle 1 Answer

NullReferenceException 1 Answer

Not sure why I'm getting a null reference exception 0 Answers

Can't access Array in a class instance multidimensional Array. 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