Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Object reference not set to an instance of an object

Discussion in '2D' started by SamVorst, Mar 3, 2020.

  1. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Hi all,

    I'm making a pac-man clone for a school project of learning C#.
    Only i got this error: Object reference not set to an instance of an object in rule 104

    I don't know what to do, this is my script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Paccie : MonoBehaviour
    5. {
    6.     public float speed = 4.0f;
    7.  
    8.     private Vector2 direction = Vector2.zero;
    9.  
    10.     private Node currentNode;
    11.  
    12.     private void Start()
    13.     {
    14.         Node node = GetNodeAtPosition(transform.localPosition);
    15.  
    16.         if (node != null)
    17.         {
    18.             currentNode = node;
    19.             Debug.Log(currentNode);
    20.         }
    21.     }
    22.  
    23.     private void Update()
    24.     {
    25.         CheckInput();
    26.  
    27.         //Move();
    28.  
    29.         UpdateOrientation();
    30.     }
    31.  
    32.     private void CheckInput()
    33.     {
    34.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    35.         {
    36.             direction = Vector2.left;
    37.             MoveToNode(direction);
    38.  
    39.         }
    40.         else if (Input.GetKeyDown(KeyCode.RightArrow))
    41.         {
    42.             direction = Vector2.right;
    43.             MoveToNode(direction);
    44.  
    45.         }
    46.         else if (Input.GetKeyDown(KeyCode.UpArrow))
    47.         {
    48.             direction = Vector2.up;
    49.             MoveToNode(direction);
    50.  
    51.         }
    52.         else if (Input.GetKeyDown(KeyCode.DownArrow))
    53.         {
    54.             direction = Vector2.down;
    55.             MoveToNode(direction);
    56.         }
    57.     }
    58.  
    59.     private void Move()
    60.     {
    61.  
    62.         transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime;
    63.     }
    64.  
    65.     private void MoveToNode (Vector2 d)
    66.     {
    67.         Node moveToNode = CanMove (d);
    68.  
    69.         if (moveToNode != null)
    70.         {
    71.             transform.localPosition = moveToNode.transform.position;
    72.             currentNode = moveToNode;
    73.         }
    74.     }
    75.  
    76.     private void UpdateOrientation()
    77.     {
    78.         if (direction == Vector2.left)
    79.         {
    80.             transform.localScale = new Vector3(-1, 1, 1);
    81.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    82.         }
    83.         else if (direction == Vector2.right)
    84.         {
    85.             transform.localScale = new Vector3(1, 1, 1);
    86.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    87.         }
    88.         else if (direction == Vector2.up)
    89.         {
    90.             transform.localScale = new Vector3(1, 1, 1);
    91.             transform.localRotation = Quaternion.Euler(0, 0, 90);
    92.         }
    93.         else if (direction == Vector2.down)
    94.         {
    95.             transform.localScale = new Vector3(1, 1, 1);
    96.             transform.localRotation = Quaternion.Euler(0, 0, 270);
    97.         }
    98.     }
    99.  
    100.     Node CanMove(Vector2 d)
    101.     {
    102.         Node moveToNode = null;
    103.  
    104.         for (int i = 0; i < currentNode.neighbors.Length; i++)
    105.         {
    106.             if (currentNode.validDirections [i] == d)
    107.             {
    108.                 moveToNode = currentNode.neighbors [i];
    109.                 break;
    110.             }
    111.         }
    112.         return moveToNode;
    113.     }
    114.  
    115.     private Node GetNodeAtPosition (Vector2 pos)
    116.     {
    117.         GameObject tile = GameObject.Find("Game").GetComponent<GameBoard>().board[(int)pos.x, (int)pos.y];
    118.  
    119.         if (tile != null)
    120.         {
    121.             return tile.GetComponent<Node>();
    122.         }
    123.         return null;
    124.     }
    125. }
    Can somebody help me please?

    Thanks, Sam
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Well that means exactly what it says; you're referring to a field or property that isn't assigned so it's effectively null meaning if you try to access properties or methods from that object, it cannot happen.

    Line 104:
    Code (CSharp):
    1. currentNode.neighbors.Length;
    This means that either "currentNode" or "neighbors" is null. You can set a breakpoint on this line and inspect which is null.

    At start you assign "currentNode" but you only do so if line 14 finds a node at a position. If it doesn't then it's going to be null.

    Really though, it sounds like you need to learn how to attach to the Unity editor and single-step through your code, assuming you don't already know that. This way you can easily figure how where your logic is going wrong. It's certainly quicker than waiting for someone to help you on a forum. :)

    Depending on what you want though, you might want to check if "currentNode" is null before that line 104 in the "CanMove()" method. It all depends on if you are happy and expect it to be null at that point else it's an error in your logic.
     
  3. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Hi, thanks for your reply.

    I figured it: Pac-Man has to be exactly on a node but i cant get to it!
    all objects has another null position so what to do now?

    Thanks Sam
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You should recognise that I don't have your project in front of me and I didn't even have any knowledge about it being a PacMan game etc etc. There's simply no way to help you if you don't explain a specific problem. The only information you've provided is the code above.
     
  5. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    That's right, I tell you something more.
    The food are called the nodes, if Pac-Man is on a specific node the inspector should display on which node Pac-Man is. Only the problem is: I can't get Pac-Man excatly on that node. So it doesn't give any info about which node Pac-Man is. With that info can i go further, but I don't know how to place Pac-Man excatly on that node and that the node Pac-man recognize as: oh here Pac-Man found at node ....
    Oh and each different sort of object like walls and nodes and Pac-Man does all have another null point in position. so I can't say Pac-Man go on that position which is equal to that node, because the values are different.

    I hope i gave enough info for you so that you understand my problem.

    Thanks Sam