c# - Constructor not working as expected in Unity -
i'm trying create griddata data in update/awake function test. can't seem constructor working. i'm new csharp , unity. so, i'm bit stuck here.
griddata class
[system.serializable] public class griddata { [system.serializable] public struct rowdata{ public float[] colum; } public static int numrows =30; public static int numcolums =20; public rowdata[] rows = new rowdata[numrows]; // //constructor public griddata(int x, int y){ numrows =y; numcolums = x; rowdata[] rows = new rowdata[numcolums]; } }
factalmapdata class
using system.collections; using system.collections.generic; using unityengine; public class fractalmapdata : monobehaviour { public int gridwidth =20; public int gridwhight =20; public griddata fractalgrid ; void update () { //test fractalgrid =new griddata(gridwidth,gridwhight); debug.log ("row" + fractalgrid.rows.length); //object reference not set instance of object debug.log ("colum" + fractalgrid.rows[0].colum.length);//object reference not set instance of object } }
you never initialize public float[] colum;
anywhere in code.
as result, when invoke constructor, although correctly create initialized rows
array, colum
field of each rowdata
instance has not been initialized , throw null reference exception when trying access length of collection.
Comments
Post a Comment