Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jfielding74 · Oct 25, 2014 at 10:51 PM · classinheritance4.6multidimensional arraysubclass

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;
             }
         }
     }
 
 }


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Oct 25, 2014 at 11:01 PM 0
Share

Variables for inheritance should be protected, not private

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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; }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges