Generic List in static function

Hi, I’m working with static functions, and in this sample code there is my test class.

// SimpleClass.js
#pragma strict
import System.Collections.Generic;

static var randomnumbers:List.<int>;
static var randomnumber:int;

	static function RandomOne()
	randomnumber = Random.Range(0,11);
	return randomnumber;
	static function RandomList(howmany:int)
		for (var i = 0;i<howmany;i++)
		var tempvalue:int = Random.Range(0,11);
	return randomnumbers;

Now, when I call the first method, the one returning a random int, no problems

number = SimpleClass.RandomOne();

the problems shows up when I try to call the second method, the one returning a populated list of random ints

somerandomnumbers = SimpleClass.RandomList(5);

it yields “NullReferenceException: Object reference not set to an instance of an object”
upon trying to add an element to the static array.
Don’t get me wrong, I don’t really need that array to be static, it’s just that as far as i can see, static methods, can only deal with static variables.
And what i’m trying to achieve, is pretty much having a collection of static functions without being carried by an actual gameobject. (so delightfully calling them MyTools.someTools(something,something);

thank you ! :slight_smile:

You haven’t initialized the randomnumbers list anywhere, hence the null reference error. If you want to return a unique list every time the RandomList function is called, however, the randomnumbers variable should not be a global static variable, instead it should be a local variable declared inside the RandomList function. Same goes for the RandomOne function; there’s no reason for it to return anything if you’re using a global static variable.