0

So I am setting up a prototypie thing for my game and I wanted to save my mesh bc it really huge(about 16,000,000 vertices) and instead of 8 for loops at the start of the game I then only had 2 but my arrays throw a NullReferenceException when trying to set.

public float[,] verts = new float[1, 3];
public int[] tris = new int[1];

public void SetValues(Vector3[] vertices, int[] triangles)
{
    verts = 
        new float [vertices.Length, 3];
    tris = 
        new int [triangles.Length];

    for(int i = 0; i < verts.Length; i++)
    {
        verts[i, 0] = 
            vertices[i].x;
        verts[i, 1] = 
            vertices[i].y;
        verts[i, 2] = 
            vertices[i].z;
    }

    for(int i = 0; i < tris.Length; i++)
    {
        tris[i] = triangles[i];
    }
}

I am in complete loss of what's causing the problem

  • 2
    which line is causing issue?. check if `vertices` or `triangles` is `null` – vivek nuna Mar 08 '21 at 07:56
  • Are you sure `vertices` and `triangles` are initialized? Did you try debugging the code? The exception text contains the file name and line number where the exception occurs. Did you check that line? – Panagiotis Kanavos Mar 08 '21 at 07:58
  • @vivek nuna the error occurs in line 6 at verts as well as line 8 at tris. I have posted the entire code that uses these values the serialization process is not yet coded I made an Class hoding the values(that you see) and a static class for Serialzing the data. To my knowledge though the array should not be null since I set its length and then populate it – Kay Steinhoff Mar 08 '21 at 08:07
  • @Panagiotis Kanavos please see my comment above (I can't add two users simultaneously) – Kay Steinhoff Mar 08 '21 at 08:08
  • Sounds like either `vertices` or `triangles` is `null` as already mentioned before and therefore `vertices.Length` and `triangles.Length` throws the exception. For Unity specific -> [Debugging managed c# Code](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) – derHugo Mar 08 '21 at 08:09
  • you can put a breakpoint and see whether the value is null or not? – vivek nuna Mar 08 '21 at 08:09
  • @KaySteinhoff edit the question and put all relevant information there. Don't describe your code, post it. *Read the duplicate*. It explains how to debug your code to find the problem. We can't debug your code, with your data, to find where the exception occurs – Panagiotis Kanavos Mar 08 '21 at 08:10
  • @KaySteinhoff *every* comment in this question is already described in the duplicate. Use breakpoints. Add watch variables. *Read* the exception text, and check the line and functions that threw. Check the parameter values. Add a null check to throw an `ArgumentNullException` if the parameters are null – Panagiotis Kanavos Mar 08 '21 at 08:12
  • @KaySteinhoff `I have posted the entire code that uses these values` not really. Where's the code that *calls* `SetValueS`? Are the parameters `vertices` or `triangles` null? If there's any chance they're null at runtime, what do you want to do? Throw a more meaningful error so you can fix the calling code? Ignore the values? – Panagiotis Kanavos Mar 08 '21 at 08:14
  • @derHugo I swear every f*cking time! I called the SetValues before generating vertices (I had to do it by hand and forgot) thanks else I would have not found the problem for another half an hour or so its an noob error that I still do after 4 years of coding. I think that will haunt me for eternity – Kay Steinhoff Mar 08 '21 at 08:17

0 Answers0