Object reference error

For some reason Unity does not seem to have an reference to my Vector3 array although I instantiated it right before.

        //Declaration here
vertices = new Vector3[(width + 1) * (width + 1)];

        for (int i = 0, y = 0; y <= width; y++)
        {
            for (int x = 0; x <= width; x++)
            {
                Debug.Log(i);
//The error ocurrs here it says it doen't have a reference
                vertices[i] = new Vector3(x, noise[x, y], y);
                i++;
            }
        }

Always paste the exact error message; sometimes it means something different than you think.

Are you sure it’s about vertices and not noise? You might split that line apart so that the array references are on separate lines if there is the slightest doubt.

Yes I have tracked it back and it is about vertices for some reason

The error message is:
NullReferenceException: Object reference not set to an instance of an object

EDIT:
Alright turns out I did the classical noob mistake and wanted to use a variable beforedeclaring it. Calling the noise function before the Mesh one did wonder
Now I have to solve another problem: my polygons load wrong prob. just some tweaking around from variables

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Simplify what you’re trying to do , get it down to 2 or 3 polys or whatever the minimum set of data is that shows your issue, so you can read each line of data that comes out about them.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.