Search Unity

NullReferenceExceptions Error

Discussion in 'Scripting' started by Sir-G, Nov 5, 2020.

  1. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    Ok so I am getting the following error:

    NullReferenceException: Object reference not set to an instance of an object.
    LevelGenerator.placeElement (System.Int32 layer, System.String name, System.Int32 count, System.Single x, System.Single y, System.Single z, UnityEngine.GameObject objToPlace, UnityEngine.Vector3 scaleBy, UnityEngine.GameObject parent, System.Boolean doWeScale) (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:567)
    LevelGenerator.generateElement (System.Single x, System.Single z, System.String element, System.Single y) (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:488)


    For whatever reason it is not adding the GameObject to the mapCoords GameObject array. For whatever reason I can output the name like on line 565 but when line 566 runs I get the above error. If i comment out line 566 the program runs and instantiates the game objects and generates the level without issue. Here is the code below.:
     
    Last edited: Nov 10, 2020
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    The array "mapCoords" is null. Initialise the variable somewhere before using it e.g. in Start()
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    Yep - at no point do I see you assigning any value to any of the 4 arrays declared on lines 66-69.
     
  4. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    I am trying to assign values on lines 566 for map coords, roadCoords on 575 is disabled because it was producing the same error. The other 2 arrays are meant to be filled after the level is generated for pathfinding purposes.

    So are you saying I have to preload data into the arrays in start in order to add elements? Or are we doing it all wrong in general.

    The idea is that the arrays mapCoords and roadCoords are to be filled as the level generates because we don't know the number of required elements at the onset of the level generation because the blocks can vary in count depending on the pixel map we use.

    I get that I am apparently not understanding completely so if you guys could give me a little bit more direction it would be great.

    Thanks
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    PraetorBlue likes this.
  6. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    I was just trying to create a 2D array. I have put into Start()

    Code (CSharp):
    1. mapCoords = new GameObject[width, height];
    2. roadCoords = new GameObject[width, height];
    And it is still producing the same error. I am reading through the documentation you linked to. Should they be public instead of private static?
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You made sure to do that before calling generateTerrain()? Post the whole Start() function.
     
  8. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    Code (CSharp):
    1. void Start()
    2.     {
    3.         mapCoords = new GameObject[width, height];
    4.         roadCoords = new GameObject[width, height];
    5.         treeCount = 0;
    6.         roadCount = 0;
    7.         castleCount = 0;
    8.         clutterCount = 0;
    9.         groundCount = 0;
    10.         waterCount = 0;
    11.         houseCount = 0;
    12.         towerSpotCount = 0;
    13.         blockCount = 0;
    14.         waypointCounter = 0;
    15.         generateTerrain();
    16.     }
     
  9. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Post the entirety of the error message you are now getting with the full stack trace.
     
  10. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object.
    2. LevelGenerator.placeElement (System.Int32 layer, System.String name, System.Int32 count, System.Single x, System.Single y, System.Single z, UnityEngine.GameObject objToPlace, UnityEngine.Vector3 scaleBy, UnityEngine.GameObject parent, System.Boolean doWeScale) (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:568)
    3. LevelGenerator.generateElement (System.Single x, System.Single z, System.String element, System.Single y) (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:490)
    4. LevelGenerator.generateTerrain () (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:431)
    5. LevelGenerator.createTerrain () (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:231)
    6. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0)
    7. Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    8. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0)
    9. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <fb001e01371b4adca20013e0ac763896>:0)
    10. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer.InvokeButton () (at <5890bae5b1944292a9144c575f50aefa>:0)
    11. UnityEngine.Debug:LogException(Exception)
    12. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:InvokeButton()
    13. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:DrawNormalButton()
    14. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:DrawPropertyLayout(GUIContent)
    15. Sirenix.OdinInspector.Editor.OdinDrawer:DrawProperty(GUIContent)
    16. Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent)
    17. Sirenix.OdinInspector.Editor.InspectorUtilities:DrawPropertiesInTree(PropertyTree)
    18. Sirenix.OdinInspector.Editor.PropertyTree:Draw(Boolean)
    19. Sirenix.OdinInspector.Editor.OdinEditor:DrawTree()
    20. Sirenix.OdinInspector.Editor.OdinEditor:DrawOdinInspector()
    21. Sirenix.OdinInspector.Editor.OdinEditor:OnInspectorGUI()
    22. UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
    23.  
     
    Last edited: Nov 5, 2020
  11. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    I see.

    It appears that Odin Inspector is invoking the "createTerrain" function before Start has even had a chance to run. Are you trying to create the terrain outside of play mode e.g. with an inspector button or something?
     
    PraetorBlue likes this.
  12. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    Thats exactly what I am trying to do
     
  13. Sir-G

    Sir-G

    Joined:
    Mar 12, 2017
    Posts:
    44
    Well I am making progress I guess IDK why arrays and c# are so unfriendly but now I am getting IndexOutOfRange error

    Code (CSharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2. LevelGenerator.placeElement (System.Int32 layer, System.String name, System.Int32 count, System.Single x, System.Single y, System.Single z, UnityEngine.GameObject objToPlace, UnityEngine.Vector3 scaleBy, UnityEngine.GameObject parent, System.Boolean doWeScale) (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:571)
    3. LevelGenerator.generateElement (System.Single x, System.Single z, System.String element, System.Single y) (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:493)
    4. LevelGenerator.generateTerrain () (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:434)
    5. LevelGenerator.createTerrain () (at Assets/Scripts/RabidGames/Generators/LevelGenerator.cs:231)
    6. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0)
    7. Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    8. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0)
    9. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <fb001e01371b4adca20013e0ac763896>:0)
    10. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer.InvokeButton () (at <5890bae5b1944292a9144c575f50aefa>:0)
    11. UnityEngine.Debug:LogException(Exception)
    12. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:InvokeButton()
    13. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:DrawNormalButton()
    14. Sirenix.OdinInspector.Editor.Drawers.DefaultMethodDrawer:DrawPropertyLayout(GUIContent)
    15. Sirenix.OdinInspector.Editor.OdinDrawer:DrawProperty(GUIContent)
    16. Sirenix.OdinInspector.Editor.InspectorProperty:Draw(GUIContent)
    17. Sirenix.OdinInspector.Editor.InspectorUtilities:DrawPropertiesInTree(PropertyTree)
    18. Sirenix.OdinInspector.Editor.PropertyTree:Draw(Boolean)
    19. Sirenix.OdinInspector.Editor.OdinEditor:DrawTree()
    20. Sirenix.OdinInspector.Editor.OdinEditor:DrawOdinInspector()
    21. Sirenix.OdinInspector.Editor.OdinEditor:OnInspectorGUI()
    22. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    23.  
    Here is my code now with the updated changes
     
    Last edited: Nov 10, 2020
  14. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You are creating new arrays for mapCoords and roadCoords, but you are not setting them to the correct size. You create the arrays using the width/height variables

    Code (CSharp):
    1.         mapCoords = new GameObject[width, height];
    2.  
    But then use the "map" textures height/width when iterating to access the array.

    Code (CSharp):
    1.         for (int hcount = 0; hcount < map.height; hcount++)
    2.  
    These values are almost certainly not the same, and as such you try to access parts of the array that don't exist. You need to either ensure that width and height are the correct value before running (which might not work, as you seem to be using the width and height variables to pass the iterators current value to the placePath function, and not to store the actual height or width of anything), or set the size of the array based on what you are actually going to try and access:

    Code (CSharp):
    1. mapCoords = new GameObject[map.width, map.height];