Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object reference error

Discussion in 'Scripting' started by TheOtherUserName, Feb 25, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    For some reason Unity does not seem to have an reference to my Vector3 array although I instantiated it right before.
    Code (CSharp):
    1.         //Declaration here
    2. vertices = new Vector3[(width + 1) * (width + 1)];
    3.  
    4.         for (int i = 0, y = 0; y <= width; y++)
    5.         {
    6.             for (int x = 0; x <= width; x++)
    7.             {
    8.                 Debug.Log(i);
    9. //The error ocurrs here it says it doen't have a reference
    10.                 vertices[i] = new Vector3(x, noise[x, y], y);
    11.                 i++;
    12.             }
    13.         }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
  3. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    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
     
    Last edited: Feb 25, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    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://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    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.