I have a class called road, as you can see below :
public class Road {
public int typeIndex;
public int[] intersection;
public Bezier[] spline;
}
I create an array of this class, using :
Road[] outputRoad = new Road[1];
Within that class, you can see a variable called Bezier, another array of a custom class. This class is :
public class Bezier {
public Vector3[] controlPoint;
public float length;
public BoundingBox boundary;
}
And within that is an array of Vector3 called controlPoints. Within my code, I need to set controlPoints, however, as expected the code throws up the error
NullReferenceException: Object reference not set to an instance of an object
System.Array.Resize[Bezier] (.Bezier[]& array, Int32 newSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1913)
roadTool_cs.BuildRoad (.buildInfo roadInfo) (at Assets/toolManager/roadTool_cs.cs:170)
roadTool_cs.Update () (at Assets/toolManager/roadTool_cs.cs:84)
Since I haven’t allocated memory to the arrays. Now I tried doing this :
System.Array.Resize (ref outputRoad, 1);
System.Array.Resize (ref outputRoad[0].spline, 1);
System.Array.Resize (ref outputRoad[0].spline[0].controlPoint, 4);
To set the size of the arrays, however on the second line, I get the exact same error as the one above, and I have no idea why. I set the size of every array from the road class, the Bezier class, and also the array of Vector3’s. What am I doing wrong? I had an idea, to put a method within the class, and write something like:
outputRoad = new Road[1];
but I STILL get an error. Anyway thanks for your time, hope you can help. ![]()
Your array of of controlPoint have the size 0, but this should throw you a Index out of range, in case you tried to assing something to it. And your Bezier array is also size 0 Your Road array is a nullRef, because in this code you posted, you never actualy put a road in the array, you just allocate a Road array of size 1. But i can't say much because i'm solo based on what you have posted here, i don't know if you fill this arrays latter, but in the state they are, they won't work.
– EDevJogosI create a new Road variable higher up in the script, but at that point in time, the array wouldve been 0 in length. Youre correct, i need to create an instance of it afterwards. Ill see if that works.
– joshua-lyness