Right now I have a base class called Tile that doesn’t derive from monobehaviour. It has a private GameObject with public getter and setter. I have another class called StoneTile that inherits from Tile where I set the GameObject via Resouces.Load();. Then I have an actual game script which creates an instance of StoneTile and instantiates StoneTile’s GameObject… But it causes a NullRefrenceException. I’m trying this new method of clean code and inheritance so I’m a bit lost. Any tips or ideas will help. Thanks
using UnityEngine;
using System.Collections;
public class Tile
{
private string type = "TILE";
private float speedBoost = 0f;
private float jumpBoost = 0f;
private float clickIncrease = 0.1f;
private float naturalIncrease = 0.1f;
private GameObject go;
public string Type
{
get
{
return type;
}
set
{
type = value;
}
}
public float SpeedBoost
{
get
{
return speedBoost;
}
set
{
speedBoost = value;
}
}
public float JumpBoost
{
get
{
return jumpBoost;
}
set
{
jumpBoost = value;
}
}
public float ClickIncrease
{
get
{
return clickIncrease;
}
set
{
clickIncrease = value;
}
}
public float NaturalIncrease
{
get
{
return naturalIncrease;
}
set
{
naturalIncrease = value;
}
}
public GameObject GO
{
get
{
return go;
}
set
{
go = value;
}
}
}
using UnityEngine;
using System.Collections;
public class StoneTile : Tile
{
public StoneTile()
{
Type = "Stone";
SpeedBoost = 0.2f;
JumpBoost = 0;
ClickIncrease = 0.2f;
NaturalIncrease = 0.1f;
GO = Resources.Load("Stone") as GameObject;
}
}
~~~~
using UnityEngine;
using System.Collections;
public class WorldCreate : MonoBehaviour {
public int width;
public int height;
public Tile[,] map;
void Start()
{
map = new Tile[width, height];
GenerateMap();
Initialize();
}
void GenerateMap()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
float r = Random.value;
if (r <= 0.25f)
{
map[x, y] = new StoneTile();
}
}
}
}
void Initialize()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
GameObject newTile = Instantiate(map[x,y].GO); ***<---- Error Here***
newTile.transform.position = new Vector3(x, 0, y);
}
}
}
}