- Home /
NullReferenceExeption when trying to put GameObject into an Array
Hello! I am currently working on a 2D TopDown Survival game, and need some help.
To get the Tile GameObject for doing stuff like digging, I save all tiles of my current chunk into an array.
For example: Tile-198-7 (Numbers are x and y positions) are saved in an array by position. This Tile would be saved in myArray[198,7].
When I try to put my tile into that array, I get that Error here:
NullReferenceException: A null value was found where an object instance was required. TerrainManagerScirpt.loadTileInArray (UnityEngine.GameObject tile, Int32 xPos, Int32 yPos) (at Assets/Scripts/Terrain/TerrainManagerScirpt.cs:44) Tiles.createTile (Int32 x, Int32 y, Int32 intTileType, UnityEngine.Sprite sprite, Int32 offset) (at Assets/Scripts/Terrain/Tiles.cs:23) TerrainManagerScirpt.Start () (at Assets/Scripts/Terrain/TerrainManagerScirpt.cs:31)
Down below are my scripts. Thanks!
Tiles
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tiles
{
//not used for now, Tile ID
int tileID;
//The Tile object
GameObject objectTile;
//Tile Sprite
Sprite tileSprite;
public Vector3 tilePos;
TerrainManagerScirpt terrainMan;
//Creates the TileObject. Uses position, what Tile it is, and the sprite used. (Tile type is only
//for reference, is not used to decide the Sprite. This is the job of the terrain Manager.
public void createTile(int x, int y, int intTileID, Sprite sprite, int offset)
{
//Gets the TerrainManager
GameObject terrainMan = GameObject.Find("TerrainManager");
TerrainManagerScirpt terrainManager = terrainMan.GetComponent<TerrainManagerScirpt>();
//Gives ID to Tile (for reference)
tileID = intTileID;
//creates the actual Tile. Tile has coordinates in its name
objectTile = new GameObject("Tile-" + x + "-" + y);
//sets offset
objectTile.transform.position = new Vector3(x - offset, y - offset, 0);
//Gives Position to Tile (for reference)
tilePos = objectTile.transform.position;
////
////
////
//This is where the GameObject is supposted to get saved into the array. It gives
//The object to the loadTileArray funktion in my TerrainManager
terrainManager.loadTileInArray(objectTile, (int)objectTile.transform.position.x, (int)objectTile.transform.position.y);
renderTile(sprite);
}
//Renders Tile, adds SpriteRenderer and sets Sprite.
public void renderTile(Sprite sprite)
{
tileSprite = sprite;
objectTile.AddComponent<SpriteRenderer>();
objectTile.GetComponent<SpriteRenderer>().sprite = tileSprite;
}
}
TerrainManagerScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainManagerScirpt : MonoBehaviour {
//Saves all Sprites in an array
public Sprite[] sprites;
//Saves tiles in an array
public GameObject[,] mapTiles;
//Sets size of Map
private int mapSize = 20;
private int x;
private int y;
//sets offset, so map gets generated "around" the player
private int offset;
public int tileType;
Tiles Tile;
void Start() {
//set Offset
offset = mapSize / 2;
//Create TileMap
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
Tile = new Tiles();
Tile.createTile(x, y, tileType, sprites[tileType], offset);
}
}
}
// Update is called once per frame
void Update() {
}
//This is used to load tiles into my array (mapTiles)
public void loadTileInArray(GameObject tile,int xPos, int yPos)
{
GameObject newTile = tile;
mapTiles[xPos, yPos] = newTile;
}
}
Your answer
Follow this Question
Related Questions
NullReferenceException: Array C# 1 Answer
Array items null but appear normal in inspector 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers