- Home /
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?
Which line is throwing the exception? If this script is attached to multiple GameObjects, is the array populated on all of them?
The line throwing the exception is the party.push at the end.
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.
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
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!