- Home /
Question by
gdg · Jul 23, 2015 at 05:26 PM ·
editor-scriptingscriptableobject
Unable to assign array data on scriptable object
I am trying to create a scriptable object via editor menu to store some data. When I try to assign ordinary fields I don't get any problems but if I try to assign an array I get a NullReferenceException.
The following code works:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[System.Serializable]
public class EachLevel
{
public uint mLevelID;
public uint mSceneID;
};
[System.Serializable]
public class LevelData : ScriptableObject
{
public EachLevel lvl;
public void Init ( uint id1, uint id2 )
{
lvl.mLevelID = id1;
lvl.mSceneID = id2;
}
}
But this one does not:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[System.Serializable]
public class EachLevel
{
public uint mLevelID;
public uint mSceneID;
};
[System.Serializable]
public class LevelData : ScriptableObject
{
public EachLevel[] lvl;
public void Init ( uint id1, uint id2 )
{
lvl = new EachLevel[2];
lvl[0].mLevelID = id1;
lvl[0].mSceneID = id2;
lvl[1].mLevelID = id2;
lvl[1].mSceneID = id1;
}
}
Any idea why?
Comment
Best Answer
Answer by gdg · Jul 24, 2015 at 07:51 AM
I got an answer by asking this question on the forum if anyone is interested. Here's the link: http://forum.unity3d.com/threads/help-with-assigning-array-data-on-scriptable-object.343220/ Basically I need to create a new EachLevel object for every element in the array like so:
lvl[0] = new EachLevel();
lvl[1] = new EachLevel();