- Home /
Unity 4.6b20 - Inheritance not working?
Hi all. Got an issue with a class within a class not inheriting a value defined in the parent class. So far I have worked out that the DataMap class does not inherit the value 'mapSize' and thus the terrainTable array is initialised at size 0,0. It was working at one point; I don't know if I changed anything or it broke on its own - most likely the former. Any help is greatly appreciated!
public class TileData : MonoBehaviour {
// attributes
private Vector2 mapSize = new Vector2();
private int tileRes;
DataMap dataMap;
public enum terrainTypes {
dirt = 0
}
class DataMap : TileData { // class holds a series of arrays that of metadata for all the tiles
public int[,] terrainTable;
public DataMap(){ // constructor : error, not inheriting!
terrainTable = new int[(int)mapSize.x,(int)mapSize.y]; // value of mapSize seems to not be inherited
Debug.Log("Data map made!");
}
}
// methods
public void OnSubmitClicked () {
Debug.Log("Submit Clicked!");
mapSize.x = int.Parse (xInput.text);
mapSize.y = int.Parse (yInput.text);
if (mapSize.x < 100 || mapSize.y < 100 ) {
ErrorReport("Out of Range");
} else {
Debug.Log ("Tile resolution is " + tileRes);
dataMap = new DataMap();
MakeAllTiles();
}
}
void MakeAllTiles(){ // makes all the tiles to the default value of dirt
Debug.Log ("The map dimensions are: " + mapSize); Debug.Log (dataMap.terrainTable.Length + " tiles to be generated"); // returns "0 tiles to be generated"
for (int x = 0; x < (int)mapSize.x; x++) {
for (int y = 0; y < (int)mapSize.y; y++) {
dataMap.terrainTable[x,y] = (int)terrainTypes.dirt;
}
}
}
}
Variables for inheritance should be protected, not private
Answer by Landern · Oct 25, 2014 at 11:02 PM
The value will not be Inherited from the outer class, techincally the Vector2 will be default, so zero's for x, y. If you want to pass in the Vector2 to the constructor, you could, you should be careful about stack over flow exceptions however.
// snip
public void OnSubmitClicked () {
Debug.Log("Submit Clicked!");
mapSize.x = int.Parse (xInput.text);
mapSize.y = int.Parse (yInput.text);
if (mapSize.x < 100 || mapSize.y < 100 ) {
ErrorReport("Out of Range");
} else {
Debug.Log ("Tile resolution is " + tileRes);
dataMap = new DataMap(this.mapSize);
MakeAllTiles();
}
}
// Another snip
public DataMap(Vector2 mappySize){ // constructor : error, not inheriting!
terrainTable = new int[(int)mappySize.x,(int)mappySize.y];
Debug.Log("Data map made!");
}
Here's a link to StackOverFlow where Jon Skeet and Hans cover this
And i quote:
In the C# language, nested classes have no special relationship with the class in which they are nested. It is a completely different type. There is only one good reason you'd ever do this: you can declare the class private. Which helps you to create a little worker class to get a job done on behalf of the outer class, a class that is completely invisible from the outside. Very useful, you cannot otherwise declare a private class at outer class scope, the best you can do is internal.
What follows is that it in no way plays a role in the inheritance of the outer class. A class you derive from the outer has no visibility to the nested class inside the base class at all. Which was the intention, declaring it private was the reason to nest it in the first place.
Now a warning, be mindful of the 'is a, is a, is a... to infinity', you can create stack over flow exceptions with ease.
An example i whipped up:
program.cs
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XMenFirstClass x = new XMenFirstClass();
x.Number2.BaconString = "Second Movie";
Console.WriteLine(x.Number2.BaconString);
}
}
}
XMenFirstClass.cs
namespace ConsoleApplication1
{
public class XMenFirstClass
{
public class XMenNextClass : XMenFirstClass
{
public string BaconString { get; set; }
}
public XMenFirstClass()
{
Number2 = new XMenNextClass();
}
public XMenNextClass Number2 { get; set; }
}
}
Your answer
Follow this Question
Related Questions
Component Class Hierarchy 2 Answers
Using AddComponent to add a Sub Class using a String 4 Answers
C# Parent-SubClass set inherited properties help 1 Answer
Inheritance best solution to handle *many* instances of a class with unique values? 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer