Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So, I do this:

var intlist:List.<System.Int32>;
intlist.Add(1);

And where I say intlist.Add(1) I get:

NullReferenceException: Object reference not set to an instance of an object

Weird, huh? Apparently, the error is .NET related and List is a .NET thing. So, please clarify the problem. Thank you.

EDIT:

The "add comment" button isn't working, so I will respond in the form of an edit. No, I can't use a JavaScript array because for whatever reason it screws up in the context of my code and the Unity people say I shouldn't use them. The syntax I am using is correct (as far as I know) in JavaScript, but I don't know if it would be null or not. Obviously, it starts out empty. That's sort of the point.

EDIT 2:

It looks like I can't accept answers either. Thanks to the both of you. It turns out I did need to call the constructor, despite some rather strange syntax errors I was getting earlier. Blowing your magic fairy dust on it made the problem go away.

share|improve this question

closed as off-topic by Byte56 Nov 22 '13 at 14:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

    
i extended my answer, this should provide you with a solution. Nevertheless you should read up on some additional resources. The articel i linked is a good startign point. In generell ArrayList and Hashtables are better in unity, as they are wrapped by the UnityScript implementation. –  floAr Nov 22 '13 at 12:09
    
Also the reason you are unable to comment yet is due to your karma score called Reputation. Accepting answers and participating in the site will gain you rep points and you can comment on other posts at 50 rep points. I added an answer myself just to encapsulate what you need –  Blue Nov 22 '13 at 12:40

2 Answers 2

Seems like the list you are adding to is still null. In c# you would need to do something along var intList=new List<System.Int32> but I am not totally sure with JS.

I am more used to the array as a list element in JS, maybe take a look at this article

var intlist=new Array() //1) regular array. Pass an optional integer argument to control array's size.
intlist[0]=1;

Edit regarding your edit:

Just to clarify: There is a huge difference between a list beeing null or empty. When you are creating your var, you basically just tell the code that you want to use a list somewhere. This object ist still null, which means there is no memory allocated for it. You need to create the object (again in C#) usally by using the constructor. The list constructor will yield you an empty (but no longer null) list. To this list you may add objects.

This is your error, accoidng to this article about collections in unity your syntax is not wrong, but it is incomplete. See this snipped from the article:

var myList : List.<Type> = new List.<Type>();  // declaration
var someNumbers = new List.<int>();            // a real-world example of declaring a List of 'ints'
var enemies = new List.<GameObject>();         // a real-world example of declaring a List of 'GameObjects'
myList.Add(theItem);                           // add an item to the end of the List
myList[i] = newItem;                           // change the value in the List at position i
var thisItem = List[i];                        // retrieve the item at position i
myList.RemoveAt(i);                            // remove the item from position i

As you notice you need to initalize the list first via constructor, by calling it with the new keyword.

share|improve this answer

I got this fully functioning in Unity on an empty game object, you need to make sure you make the list with the new statement and also ensure you have the imports correctly defined.

#pragma strict
import System.Collections.Generic;
import System.Linq;

function Start () {
    var intlist : List.< System.Int32 > = new List.< System.Int32 >() ;
    intlist.Add(42);
    Debug.Log(intlist[0] ); //Outputs 42
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.