- Home /
Set Specific List Value
This is a very simple question, but I'm stuck on it and I can't find the right answer.
Here goes,
private var Filenames : List.<String>;
function Start () {
Filenames[4] = "test";
print (Filenames[4]);
}
It always prints out as "Null Reference Exception, hinting to the Filenames[4] = "test"; line.
How do I get it to work?
Thanks!
make Filename public and then define the size of list in inspector for ex. 5 and then run it ... hope it will help u
Answer by Olgo · Apr 15, 2015 at 04:26 AM
I'm not used to javascript, but I would have to say you need declare the list before referencing objects in it.
var Filenames = ["zero", "one", "two", "three", "test"];
Above you're looking for the fourth index of the list, when there's nothing in the list yet.
If it were c# you would need List Filenames = new List(); This populates the list, while just saying List Filenames; gets you nothing but null reference exceptions.
Answer by Tomer-Barkan · Apr 15, 2015 at 04:37 AM
List probably needs adding elements in order to work properly. consider using an array:
var x = [];
x[4] = "aaa";
print(x[4]);
Your answer
Follow this Question
Related Questions
Finding the index of the min value in the list 1 Answer
A node in a childnode? 1 Answer
Insert a string to a list inside a list 3 Answers
FindIndex in a List 1 Answer