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

Arrays Issue

Discussion in 'Scripting' started by theZiggy1, Jan 16, 2020.

  1. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    I have two snippets of code, one which is the main function, Run(). The idea is to loop through a Tileset, and input each of the tilebases into my created class. This should happen at
    inputMapContainer[x, y].SingleTile = inputTiles[x, y];

    however i instead get a NullReferenceException: Object not set to an instance of an object.

    What am I missing here? Also please ignore the setting 1d array into a 2-d array first, this was to be a work around if i could, but alas didnt work.

    Code (CSharp):
    1.   [SerializeField] Tilemap InputMap;
    2.    [SerializeField] Tilemap outputMap;
    3.    public  TileBase[] allTiles;
    4.    public BoundsInt bounds;
    5.    public TileBase[,] inputTiles;
    6.    public TileContainer[,] inputMapContainer;
    7.  
    8. void Run()
    9.     {
    10.         InputMap.CompressBounds();
    11.         bounds = InputMap.cellBounds;
    12.         TileBase[] allTiles = InputMap.GetTilesBlock(bounds);
    13.         inputTiles = new TileBase[bounds.size.x,bounds.size.y];
    14.         inputMapContainer = new TileContainer[bounds.size.x, bounds.size.y];
    15.  
    16.         for(int x = 0; x < bounds.size.x; x++)
    17.             for(int y = 0; y < bounds.size.y; y++)
    18.             {
    19.                 if (allTiles[x + y * bounds.size.x] == null)
    20.                 {
    21.                     Debug.Log("Null tile, will not work, location: " + x + " and " + y);
    22.                 }
    23.              
    24.                   inputTiles[x, y] = allTiles[x + y * bounds.size.x];
    25.                 Debug.Log(inputTiles[x, y].name + " this is a 2D array" + x + " " + y);
    26.                 // Debug.Log(allTiles[x + y * bounds.size.x].name);
    27.                 inputMapContainer[x, y].SingleTile = inputTiles[x, y];
    28.             }
    29.     }
    Code (CSharp):
    1. public class TileContainer : MonoBehaviour
    2. {
    3.  
    4.     public TileBase SingleTile;
    5.     List<TileBase> possibleUpTiles;
    6.     List<TileBase> possibleDownTiles;
    7.     List<TileBase> possibleLeftTiles;
    8.     List<TileBase> possiblRightTiles;
    9.     List<TileBase> possibleTilesForThis;
    10.     bool hasBeenCollapsed = false;
    11.     bool hasCollision = false;
    12. }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    What line is giving you the NRE?
     
  3. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    Line 27.
     
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Ah! When you make an array of objects (like your TileContainer), C# does not fill the slots of the array with objects; each slot is null. You have to create the object first, then add it to that index.
    I also see that your TileContainer is a monobehaviour. You might have to use something else, like scriptable objects because Monos are intended to be components attached to gameobjects.
     
  5. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    I would guess then in the line before if i went

    inputmapContainer[x,y] = new TileContainer;

    i also added a quick constructor to the script, and it seems have fixed it.
    that would also solve my issue?
     
    Last edited: Jan 16, 2020
    WallaceT_MFM likes this.
  6. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    So my solution was to add a constructor for TileContainer
    Code (CSharp):
    1.   public TileContainer(TileBase newTile)
    2.     {
    3.         SingleTile = newTile;
    4.     }
    and change the line at 27 to

    inputMapContainer[x, y] = new TileContainer(inputTiles[x, y]);