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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Null Reference Exception Help

Discussion in 'Scripting' started by Forcetree, Mar 12, 2017.

  1. Forcetree

    Forcetree

    Joined:
    Mar 12, 2017
    Posts:
    1
    Hello. My name is Forest and I am fairly new to unity. i have solved issues similar to this one in the past but I can't quite understand how to fix this issue. The code is fairly elaborate and so I have included all components I think are necessary and hope it's not too excessive.

    Essentially I am creating a board game in Unity for my studies. The units are created and handled instances of the Unit class, tiles by Tile class, and the whole thing is handled by a master script. Units are moved from tile to tile and references are placed inside an internal PlayerPlacematClass of the Tile class. After I instantiate and attempt to move my first player into a PlayerPlaceMat of the first tile this error occurs.

    A function of the PlayerPlaceMat class is called by a unit passing "this" as the parameter for newUnit and it is this parameter that is causing the Null Reference error claiming that the Unit is not set to the instance of an object. How is this possible when the object itself is passing the "this" argument to the MoveIn() function I have defined.

    See my code below:

    Master Script:

    Code (CSharp):
    1. bool CreatePlayer (int playerNum) //This function will only be able/made to create players not enemies
    2.     {
    3.            
    4.         Unit unitInstance = (Unit)Instantiate(BasicUnit, new Vector3(0, -5, 0), Quaternion.identity);
    5.  
    6.         Unit.playerType type;
    7.  
    8.         switch (playerNum)
    9.         {
    10.             case 1:
    11.                 type = Unit.playerType.engineer;
    12.                 break;
    13.             case 2:
    14.                 type = Unit.playerType.hunter;
    15.                 break;
    16.             case 3:
    17.                 type = Unit.playerType.nurse;
    18.                 break;
    19.             case 4:
    20.                 type = Unit.playerType.scout;
    21.                 break;
    22.             default:
    23.                 type = Unit.playerType.empty;
    24.                 return false;
    25.         }
    26.  
    27.         unitInstance.Initialize(Unit.unitType.player, playerNum, type);
    28.  
    29.         unitInstance.name = "Player " + playerNum;
    30.  
    31.         Debug.Log("Player created: " + unitInstance.name);
    32.  
    33.         if (unitInstance.MovePlayer(0)) //Bounce player from null tile (tile really doesn't exist: see is touching protocol) ONLY TIME MOVE 0 IS USED IS TO PLACE UNIT ON START TILE
    34.         {
    35.             return true;
    36.         }
    37.         else
    38.         {
    39.             return false;
    40.         }
    41.    
    42.     }
    Unit Class:

    Code (CSharp):
    1. public bool MovePlayer (int dir)
    2.     {
    3.         Debug.Log("MovePlayer Reached");
    4.  
    5.         Tile mTile;
    6.  
    7.         Debug.Log("Performing move function on Player " + playerID + " whose current tile ID is " + cTileID);
    8.  
    9.         mTile = cTile.IsTouching(dir);
    10.  
    11.         if (mTile != null)
    12.         {
    13.             if (mTile.isFlipped == true)
    14.             {
    15.  
    16.                 if (!mTile.MoveIn(this))
    17.                 {
    18.                     Debug.Log("Couldn't move in!");
    19.                     return false;
    20.                 }
    21.                 if (!cTile.MoveOut(this))
    22.                 {
    23.                     Debug.Log("Couldn't move out!");
    24.                     return false;
    25.                 }
    26.            
    27.             }
    28.             else
    29.             {
    30.                 return false;
    31.             }
    32.         }
    33.         else
    34.         {
    35.             return false;
    36.         }
    37.  
    38.         return true;
    39.     }
    Tile Class: (Placemat is internally defined)

    Code (CSharp):
    1. //                          //
    2.     //      Placemat Class      //
    3.     //                          //
    4.  
    5.     public class PlayerPlaceMat
    6.     {      
    7.  
    8.         private static Vector2[] prisoner = new Vector2[] { new Vector2(-2, 2), new Vector2(2, 2), new Vector2(-2, -2), new Vector2(2, -2) };
    9.         private static Vector2[] stand = new Vector2[] { new Vector2(-2, 0), new Vector2(-2, 0) };
    10.  
    11.         int size;
    12.         Vector2 positionN;
    13.  
    14.         //      INTERNAL CLASS      //
    15.         internal class Slot
    16.         {
    17.             private Unit hUnit; //hUnit for held unit
    18.             public bool filled = false;
    19.  
    20.             public void In(Unit newUnit)
    21.             {
    22.                 hUnit = newUnit;
    23.                 filled = true;
    24.             }
    25.             public Unit Out()
    26.             {
    27.                 filled = false;
    28.                 return hUnit;
    29.             }
    30.             public bool Check(Unit newUnit)
    31.             {
    32.                 if (hUnit == newUnit)
    33.                 {
    34.                     return true;
    35.                 }
    36.                 else
    37.                 {
    38.                     return false;
    39.                 }
    40.             }
    41.             public void Dif(int place, Vector2 pN, int s)
    42.             {
    43.                 if (s == 4)
    44.                 {
    45.                     hUnit.newPos = pN + prisoner[place];
    46.                 }
    47.                 else if (s == 2)
    48.                 {
    49.                     hUnit.newPos = pN + stand[place];
    50.                 }
    51.                 else
    52.                 {
    53.                     Debug.Log("Placemat size is " + s + " and wasn't recognized");
    54.                 }
    55.             }
    56.         }
    57.  
    58.         private Slot[] mat;
    59.  
    60.         public PlayerPlaceMat(int slots, Vector2 pos)
    61.         {
    62.             mat = new Slot[slots];
    63.             size = slots;
    64.             positionN = pos;
    65.         }
    66.  
    67.         public bool Push(Unit newUnit)
    68.         {
    69.             for (int i = 0; i < size; i++)
    70.             {
    71.                 if (!mat[i].filled)
    72.                 {
    73.                     mat[i].In(newUnit);
    74.                     mat[i].Dif(i, positionN, size);
    75.                     sort();
    76.                     return true;
    77.                 }
    78.             }
    79.  
    80.             return false;
    81.         }
    82.  
    83.         public bool Pull(Unit newUnit)
    84.         {
    85.             for (int i = 0; i < size; i++)
    86.             {
    87.                 if (mat[i].filled && mat[i].Check(newUnit))
    88.                 {
    89.                     mat[i].Out();
    90.                     sort();
    91.                     return true;
    92.                 }
    93.             }
    94.  
    95.             return false;
    96.         }
    97.  
    98.         private void sort()
    99.         {
    100.             //No usage yet: Optional aesthetic upgrade
    101.         }
    102.  
    103.     }
    104.  
    105.  
    106.     //      End Class Arcitecture       //
    107.  
    108.     private PlayerPlaceMat cMat;
    109.  
    110.     void Start ()
    111.     {
    112.         sr = GetComponent<SpriteRenderer> ();
    113.  
    114.         tilePos = this.transform.position;
    115.  
    116.         if (type == tileType.start)
    117.         {
    118.             cMat = new PlayerPlaceMat(4, tilePos);
    119.         }
    120.         else
    121.         {
    122.             cMat = new PlayerPlaceMat(2, tilePos);
    123.         }
    124.  
    125.     }
    126.  
    127.     void Update ()
    128.     {
    129.    
    130.     }
    131.  
    132.     public bool MoveIn(Unit nUnit)
    133.     {
    134.         if (!cMat.Push(nUnit))
    135.         {
    136.             return false;
    137.         }
    138.  
    139.         return true;
    140.     }
    141.  
    142.     public bool MoveOut(Unit nUnit)
    143.     {
    144.         if (!cMat.Pull(nUnit))
    145.         {
    146.             return false;
    147.         }
    148.    
    149.         return true;
    150.     }
    As I mentioned I have clipped out portions of my code and I hope I didn't exclude anything essential. If you need to know anything else please ask me and I will provide more details - I was only trying to cut down the amount of code shown for legibility. If you can't see a declaration or initialization assume I have done so appropriately as no errors are generated aside from this Null Reference.

    The exact error returned is as follows: (line numbers in error are of original code, please ignore)

    NullReferenceException: Object reference not set to an instance of an object
    Tile.MoveIn (.Unit nUnit) (at Assets/Scripts/Tile.cs:218)
    Unit.MovePlayer (Int32 dir) (at Assets/Scripts/Unit.cs:208)
    MasterScript.CreatePlayer (Int32 playerNum) (at Assets/Scripts/MasterScript.cs:288)
    MasterScript.Start () (at Assets/Scripts/MasterScript.cs:57)
     
    Last edited: Mar 12, 2017
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Line numbers are different because you clipped that code. Something is not assigned in Tile.cs