Search Unity

NullReferenceExeption Error that makes no sense..

Discussion in 'Scripting' started by GrafSeeger, Jul 24, 2017.

  1. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    Hello Guys,

    I am workin on a random dungeon generator but I got stuck here with the NullReferenceExeption Error(Line : 56) and I can't find a solution, because the error make's no sense for me:/

    I hope you can help me :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Roo_Controller : MonoBehaviour {
    6.     private Rooms[] RoomPrevious;
    7.     private Rooms[] RoomArr;
    8.     public GameObject PreFab;
    9.     public GameObject Parent;
    10.     public int TotalRooms;
    11.     public int RoomsA;
    12.     public int width;
    13.     public int height;
    14.     const float perc = .8f;
    15.     public int minA;
    16.  
    17.    
    18.     // Use this for initialization
    19.     void Start () {
    20.         RoomArr = new Rooms[TotalRooms];
    21.         RoomPrevious = new Rooms[TotalRooms];
    22.         RoomArr[0] = new Rooms(0, 0, 0, 0);
    23.         GenerateRooms(width, height, TotalRooms, 1);
    24.        // Debug.Log(RoomArr);
    25.         PlaceRooms();
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.        
    31.     }
    32.  
    33.     void GenerateRooms(int w, int h, int tamount,int amount)
    34.     {
    35.         RoomPrevious[0] = RoomArr[0];
    36.         for (int s = 0; s < tamount; s++)
    37.         {
    38.             int wd = Random.Range(3, 6);
    39.             int ht = Random.Range(3, 6);
    40.             int PosX = Random.Range(0, w - wd);
    41.             int PosY = Random.Range(0, h - ht);
    42.            
    43.  
    44.             if (wd * ht > minA && PlaceFree(s))
    45.                 {
    46.  
    47.                     RoomArr[s] = new Rooms(wd, ht, PosX, PosY);
    48.                     //Debug.Log(RoomArr[s]);
    49.                     RoomPrevious[s] = new Rooms(wd, ht, PosX, PosY);
    50.             }
    51.            
    52.          
    53.         }
    54.     }
    55.  
    56.     public bool PlaceFree(int d) //-------Here is the error--------
    57.     {
    58.         for (int h = 0; h < d; h++)
    59.         {
    60.             int i = d;
    61.             //The Variable Declaration for the IF statment
    62.             int xP = RoomPrevious[i].PosX;
    63.             int yP = RoomPrevious[i].PosY;
    64.             int xEndP = xP + RoomPrevious[i].width;
    65.             int yEndP = yP + RoomPrevious[i].height; ;
    66.             int x = RoomArr[i].PosX;
    67.             int y = RoomArr[i].PosY;
    68.             int xEnd = x + RoomArr[i].width;
    69.             int yEnd = y + RoomArr[i].height;
    70.  
    71.             if (xP != x && yP != y && xEndP != xEnd && yEndP != yEnd)
    72.             {
    73.                 return true;
    74.             }
    75.             else
    76.             {
    77.                 return false;
    78.             }
    79.  
    80.         }
    81.            
    82.        
    83.     }
    84.     void PlaceRooms()
    85.     {
    86.  
    87.         for (int i = 0; i < TotalRooms; i++)
    88.         {
    89.             GenerateRoomObject(RoomArr[i]);
    90.         }
    91.  
    92.     }
    93.  
    94.     void GenerateRoomObject(Rooms A)
    95.     {
    96.         for (int i = 0; i < A.height; i++)
    97.         {
    98.             for (int j = 0; j < A.width; j++)
    99.             {
    100.                 Instantiate(PreFab, new Vector3(A.PosX + j, 0, A.PosY + i), transform.rotation);
    101.                
    102.             }
    103.         }
    104.     }
    105.     [System.Serializable]
    106.     public class Rooms
    107.     {
    108.         public int width;
    109.         public int height;
    110.         public int PosX;
    111.         public int PosY;
    112.  
    113.         public Rooms(int a, int b, int c, int d)
    114.         {
    115.             width = a;
    116.             height = b;
    117.             PosX = c;
    118.             PosY = d;
    119.         }
    120.     }
    121. }
    122.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    hmm...Well, there isn't a way for that line to be null. Can you post the entire error with the stack trace and all?
     
  3. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    NullReferenceException: Object reference not set to an instance of an object
    Roo_Controller.GenerateRoomObject (.Rooms A) (at Assets/Roo_Controller.cs:80)
    Roo_Controller.placeRooms () (at Assets/Roo_Controller.cs:73)
    Roo_Controller.Start () (at Assets/Roo_Controller.cs:24)
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    It looks like the loop in PlaceFree() only ever runs once. It either returns true or false after the first iteration.
     
  5. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    And how could I solve this problem ? :/
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    As @orb said, the PlaceFree will return t/f on the first pass. That doesn't explain your null exception on either line that you mentioned.

    Are you sure you saved the script after making changes? Those line numbers in the stack trace don't really match up correctly. (assuming this is your entire script)
     
  7. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    Ah I think the For loop is the problem
     
  8. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    The script as presented doesn't even compile for me, so you're looking at error messages from a previous version of it.
     
  9. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    So this is the newest version

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Roo_Controller : MonoBehaviour {
    6.     private Rooms[] RoomPrevious;
    7.     private Rooms[] RoomArr;
    8.     public GameObject PreFab;
    9.     public GameObject Parent;
    10.     public int TotalRooms;
    11.     public int RoomsA;
    12.     public int width;
    13.     public int height;
    14.     const float perc = .8f;
    15.     public int minA;
    16.  
    17.    
    18.     // Use this for initialization
    19.     void Start () {
    20.         RoomArr = new Rooms[TotalRooms];
    21.         RoomPrevious = new Rooms[TotalRooms];
    22.         RoomArr[0] = new Rooms(0, 0, 0, 0);
    23.         GenerateRooms(width, height, TotalRooms, 1);
    24.        // Debug.Log(RoomArr);
    25.         PlaceRooms();
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.        
    31.     }
    32.  
    33.     void GenerateRooms(int w, int h, int tamount,int amount)
    34.     {
    35.         RoomPrevious[0] = RoomArr[0];
    36.         for (int s = 0; s < tamount; s++)
    37.         {
    38.             int wd = Random.Range(3, 6);
    39.             int ht = Random.Range(3, 6);
    40.             int PosX = Random.Range(0, w - wd);
    41.             int PosY = Random.Range(0, h - ht);
    42.            
    43.  
    44.             if (wd * ht > minA && PlaceFree(s))
    45.                 {
    46.  
    47.                     RoomArr[s] = new Rooms(wd, ht, PosX, PosY);
    48.                     //Debug.Log(RoomArr[s]);
    49.                     RoomPrevious[s] = new Rooms(wd, ht, PosX, PosY);
    50.             }
    51.            
    52.          
    53.         }
    54.     }
    55.  
    56.     public bool PlaceFree(int d) //Here it tells me
    57.     {
    58.         for (int h = 0; h < d; h++)
    59.         {
    60.             int i = d;
    61.             //The Variable Declaration for the IF statment
    62.             int xP = RoomPrevious[i].PosX;
    63.             int yP = RoomPrevious[i].PosY;
    64.             int xEndP = xP + RoomPrevious[i].width;
    65.             int yEndP = yP + RoomPrevious[i].height; ;
    66.             int x = RoomArr[i].PosX;
    67.             int y = RoomArr[i].PosY;
    68.             int xEnd = x + RoomArr[i].width;
    69.  
    70.             int yEnd = y + RoomArr[i].height;
    71.  
    72.             if (xP != x && yP != y && xEndP != xEnd && yEndP != yEnd)
    73.             {
    74.                 return true;
    75.             }
    76.             else
    77.             {
    78.                 return false;
    79.             }
    80.  
    81.         }
    82.            
    83.        
    84.     }
    85.     void PlaceRooms()
    86.     {
    87.  
    88.         for (int i = 0; i < TotalRooms; i++)
    89.         {
    90.             GenerateRoomObject(RoomArr[i]);
    91.         }
    92.  
    93.     }
    94.  
    95.     void GenerateRoomObject(Rooms A)
    96.     {
    97.         for (int i = 0; i < A.height; i++)
    98.         {
    99.             for (int j = 0; j < A.width; j++)
    100.             {
    101.                 Instantiate(PreFab, new Vector3(A.PosX + j, 0, A.PosY + i), transform.rotation);
    102.                
    103.             }
    104.         }
    105.     }
    106.     [System.Serializable]
    107.     public class Rooms
    108.     {
    109.         public int width;
    110.         public int height;
    111.         public int PosX;
    112.         public int PosY;
    113.  
    114.         public Rooms(int a, int b, int c, int d)
    115.         {
    116.             width = a;
    117.             height = b;
    118.             PosX = c;
    119.             PosY = d;
    120.         }
    121.     }
    122. }
    123.  
     
  10. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    The int d in the PlaceFree bool is equal to the amount of rooms the bool has to check and so the first time it has to check 2 rooms, the second time 3 and so on.
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Ok, so if this is your new version of the script. What is the error you are getting with the stack trace? Because if the error you copied and pasted references this script...line 80 is an empty line.
     
  12. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    NullReferenceException: Object reference not set to an instance of an object
    Roo_Controller.GenerateRoomObject (.Rooms A) (at Assets/Roo_Controller.cs:107)
    Roo_Controller.PlaceRooms () (at Assets/Roo_Controller.cs:100)
    Roo_Controller.Start () (at Assets/Roo_Controller.cs:24)
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    hmm...Ok, the line numbers still don't match up, however, it looks like your instantiate call is returning null.

    Do you have something assigned to the Prefab variable?
     
  14. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
  15. GrafSeeger

    GrafSeeger

    Joined:
    Jun 22, 2016
    Posts:
    19
    Assets/Roo_Controller.cs(59,17): error CS0161: `Roo_Controller.PlaceFree(int)': not all code paths return a value
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Keep in mind if you are mixing Windows and MacOSX line endings, then the line number of the crash can be misreported.
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    This error is because your for loop may not run. Thus, if you don't enter your for loop, you will not return anything.

    So you need to return false after the for loop. Even if you never hit that point and always enter your for loop, you still have to declare a return.