Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Aruel · Oct 30, 2015 at 09:16 PM · staticinheritance

Having static classes inherit from a static class

Hidiho!

This problem is not too important right now - I could easily do it with some kind of workaround - it is more about designing my code and whether to do some fancy stuff. Wrapping it up, I'm doing some kind of hero-management game, which is mainly some GUI in different scenes. You have different heroes and let them do stuff. Every hero has a character sheet with his stats. I thought about making the heroes' stats scripts static, so that I wouldn't have to bother pushing information from scene to scene - when referencing a hero, I am always talking about the same hero anyways. Every hero has the same stats, so it seems to be a good opportunity (hey, there's UNITY in it!) to do some inheriting. Problem is: my static heroes cannot inherit from a class that's not object.

Some example code of what I would like to do:

 public static class Character : object
 {
 public static int level;
 public static int health;
 //...
 
 //some other functions they have in common, like how experience translates into levels etc.
 }
 
 public static class Aragorn : Character
 {
 level = 3;
 health = 20;
 //...
 }
 
 public static class Legolas : Character
 {
 level = 3;
 health = 15;
 //...
 }

What is a good way of doing this? I am realizing right now, that having the fields in Character won't help, because I will have to specify them again anyways, but it would be nice to have the methods where I need them. Are static classes useful in this scenario, or is there some way to do it with interfaces or other things?

I'm looking forward to any input on this whole thing

Paul

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by rageingnonsense · Oct 30, 2015 at 11:03 PM

How about something like this:

 public static class CharacterManager {
     public static List<Character> heroes;
 }
 
 public abstract class Character {        
     int level;
     float health;
 }
 
 public class Aragorn : Character {    
 }

fill in with constructors etc. The idea is to keep a static class that keeps track of each character, but the characters themselves are not static.

When you create a character; you add them to the static list. You an add methods to CharacterManager to make accessing the list nicer

This is super hobbled together, but I hope it demonstrates the basic idea. There are a lot of ways to skin this cat, and this is but one of them.

Comment
Add comment · Show 4 · 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
avatar image Aruel · Oct 31, 2015 at 07:55 AM 0
Share

Three years of studying computer science and I completely forgot about abstract classes. Tried to do it with an interface, which didn't work out either. But this is probably a nice way to have the same functionality! It's just a little sad, having to do it via some list ins$$anonymous$$d of just having the characters themselves being static - I mean, not having to think about instances and stuff when I change them is basically what I want in that situation.

On the other hand, this makes it easier to iterate through all the characters for some checks, or gives me easier ways of having multiple save files (a thing I haven't thought about yet).

Thanks so far! But I'm still interested in some other ways of cat skinning ;) Just in order to see some more examples or possibilities

avatar image Bunny83 Aruel · Oct 31, 2015 at 11:32 AM 0
Share

You can't derive a static class at all. It also wouldn't make any sense since a static class only contains static elements. Those would be shared by all derived classes. An example:

 public class BaseClass
 {
     public static int test;
     public static string s;
     static BaseClass()
     {
         test = 1;
         s = "base";
     }
 }
 public class ChildClass1 : BaseClass
 {
     public static int someOther = 44;
     static ChildClass1()
     {
         test = 4;
         s = "child";
     }
 }
 

 void Start ()
 {
     /* #1 */Debug.Log("Base.test = " + BaseClass.test + " Base.s = " + BaseClass.s);
     /* #2 */Debug.Log("Child1.test = " + ChildClass1.test + " ChildClass1.s = " + ChildClass1.s);

     /* #3 */Debug.Log("Child1.other = " + ChildClass1.someOther);

     /* #4 */Debug.Log("Child1.test = " + ChildClass1.test + " ChildClass1.s = " + ChildClass1.s);
     /* #5 */Debug.Log("Base.test = " + BaseClass.test + " Base.s = " + BaseClass.s);
 }

At #1 the static constructor of our Base class is called since we first access the class. So we get the number "1" and the string "base".

At #2 you might think that Child's static constructor will run, but that's actually wrong. You don't access the child class at all. Visual studio also shows that the access can be "simplified" and suggests to use BaseClass.test ins$$anonymous$$d of ChildClass1.test since they represent the same field. Internally you actually access "BaseClass.test" and that's why the static constructor won't run.

At #3 we actually access something from the Child1 class so the static constructor will run. It will overwrite "test" and "s".

So at #4 we will see the number "4" and the string "child"

At #5 we will also see "4" and "child" since static elements only exists once.

That's why it makes no sense at all to inherit a static class. Static classes are sealed automatically by the compiler. The only way to actualy create seperate classes is by using a generic base class and pass the derived class type as type parameter. That will create an actual seperate class which "inherits" the static members of the base class. Actually there is no base class. A generic class is not a complete class without a type for the type parameter.

However never ever think about using generics for this. This will produce tons of other problems due to the fact how static initialization works. It's still the case that when you access an "inherited" static member (that is defined in the generic base class) the static constructor of the child won't be executed until you access an actual member of the child class. So it's not possible to reliably initialize inherited members inside the child class. Initialization is done in the base class

avatar image Bunny83 Aruel · Oct 31, 2015 at 03:53 PM 0
Share

Here are some links that might help you to understand it a bit better:

  • http://stackoverflow.com/questions/774181/why-cant-i-inherit-static-classes

  • https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

avatar image Aruel Bunny83 · Nov 02, 2015 at 11:41 AM 0
Share

Thank you very much! Got it now... and looking back, it seems pretty obvious, too.

So once again: thanks guys. I think I'll go with nonsense's idea of storing them in some kind of character manager. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

inheritance and static properties 1 Answer

Question about static variables and inheritance 2 Answers

Access variables,method from a Mono Behaviour without static use. -1 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Make all instances inherit the same value from parent and make static 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