The question is answered, right answer was accepted
NullReferenceException making no sense
I have already tried a lot of debugging on this and it make no sense whatsoever. I made sure that the problem exists in the following code block, when i try to access any of the items in the array it gives me an error. I have also made sure that the array is initialized before i access it. I feel like its something stupid and for some reason i can't see it.
public void SetVertDataLength()
{
if (setVertData)
{
vertData = new VertexData[pipeSegmentCount * maxCurveSegCount];
for (int i = 0; i < vertData.Length; i++)
{
vertData[i] = new VertexData();
}
setVertData = false;
}
}
this is where i try to access the array and again i've already check that this code runs after the previous
private void CreateQuadRing(float u, int i)
{
float vStep = (2f * Mathf.PI) / pipeSegmentCount;
int ringOffset = pipeSegmentCount * 6;
VertexData vertex = new VertexData();
vertex.originalU = u;
vertex.originalV = 0f;
vertex.RandomizePosition();
for (int v = 1; v <= pipeSegmentCount; v++, i += 6)
{
// the following line is where the error occurs
vertData[i].CopyVertexData(vertData[i - ringOffset + 4]);
vertData[i + 1].CopyVertexData(vertData[i - ringOffset + 5]);
vertData[i + 2].CopyVertexData(vertData[i - ringOffset + 5]);
vertData[i + 3].CopyVertexData(vertex);
vertData[i + 4].CopyVertexData(vertex);
vertData[i + 5].originalU = u;
vertData[i + 5].originalV = v * vStep;
vertData[i + 5].RandomizePosition();
vertex = vertData[i + 5];
}
}
What have you tried to solve it?
The problem seems to lie in your VertexData type. Where does it come from? Did you implement it yourself?
Trying your code with working types like a vector3:
void Start()
{
vertData = new Vector3[3 * 4];
for (int i = 0; i < vertData.Length-1; i++)
{
vertData[i] = Vector3.zero;
}
}
I was substituting your length variables pipeSegmentCount and maxCurveSegCount with 3 and 4 because it doesn't matter for evaluation of the code.
That code will create a Vector3 array "vertData" at Start with 12 Vector3's (0,0,0) in them:
$$anonymous$$y guess is that at the moment where the line with the error is executed the referred index of
vertData[i - ringOffset + 4]
has not been initialized, or has been set to null for whatever reason.
Just a thought: I'm quite sure that the compiler would tell you if it was exceeding the array size, it would always tell you that via the "index out of range" error.
Try to print the name of the element in the array that you want to work with above that line:
print(vertData[i - ringOffset + 4]; // .name or whatever value you can access from that type.
vertData[i].CopyVertexData(vertData[i - ringOffset + 4]);
This will give you closure if that object is null or not.
Hope I could help somehow. It's difficult without knowing the datatypes in custom scripts.
Thanks for your reply, it turns out that the array was initialized twice and that ended up causing the issue. Thanks for the help tho, much appreciated.
If someone helps you, give them a upvote ;)
Follow this Question
Related Questions
#Need Fast Help# NullReferenceException: Object reference not set to an instance of an object 0 Answers
null reference help. i fundamentally dont understand. 1 Answer
null reference on instantiated prefab array at instantiation 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer