instantiate ScriptableObject, add instance to a List

This shouldn’t be so hard, but I seem to have some sort of basic missunderstanding of ScriptableObjects…

Here’s my code:

#pragma strict
import System.Collections.Generic;

private var presets		: List.<Preset>;
private var newPreset 	: Preset;
private var index 		: int = 0;


function Start () {	SetPreset(index); }

// newPreset is created, index is set,
// newPreset should be added to presets-List
function SetPreset (index : int){

	newPreset = ScriptableObject.CreateInstance(Preset);

	newPreset.index = index;
	print (newPreset.index);

	presets.Add(newPreset);  // NULL REFERENCE EXCEPTION
}

and the Preset.js:

#pragma strict
class Preset extends ScriptableObject {

	var index : int	= 0;
}

the Index gets printed all right. But the sliders.Add(newPreset); gives a NullReference.

confusion!!

You’ve declared a presets variable, but you haven’t created an instance of it. You have to create an instance before using it. Probably the best place to do that would be in Start() before calling SetPreset.

The C# syntax for this would be…

presets = new List<Preset>();

I’m not sure what the Javascript syntax is, but it should be close to that.