- Home /
The question is answered, right answer was accepted
Instantiating from an object classes GameObject property?
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);
}
}
}
}
Can you post the script where the error occurs please ? Also, indicate exactly the line causing the error.
Oops. Yes, sorry about that. I accidentally posted the same script twice. The script with the error is the last one. There is an arrow that indicates where the error is. Thanks for pointing that out
Answer by MegaChuck64 · Mar 21, 2017 at 06:59 PM
Well I got the answer from reddit you slackers! The problem was that not all Tiles in the map array were getting set so when it tried to instantiate a null Tile it would just stop running instead of simply skipping it. A simple null check fixes the problem.
Follow this Question
Related Questions
Problem with missing texture with Instantiate( Resources.load ) method 0 Answers
Instantiate a class 0 Answers
Class Organization and Inheritance 1 Answer