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 Flynn · Mar 02, 2011 at 06:48 PM · subclassing

Subclassing no-go? (C#)

Hi :)

I'm creating a game which will have many forms of the same object, with many different functionalities.

I am trying to create a skeleton class which contains all of the shared properties of these objects. Then, I define it as the superclass to all the other objects.

That works fine and well, until... Anonymous typing.

Say I have the class BaseClass.

Then I have the class SubClass, which is a subclass of BaseClass

Finally, I have TestClass, which contains one variable of type BaseClass.

If I add BaseClass to a new gameobject, and try to add the gameobject to the variable in TestClass, it works fine.

But if I instead add SubClass, it does not work at all! It simply will not attribute its self to the variable!

How in blazes can I get this to work??

3 classes:

BaseClass:

using UnityEngine; using System.Collections;

public class BaseClass : MonoBehaviour { public string mytext; public int countA = 0; void Update () { IC(); }

 public void IC()
 {
     countA++;
 }

}

SubClass:

using UnityEngine; using System.Collections;

public class SubClass : BaseClass { }

TestClass:

public class TestClass : MonoBehaviour {
    public BaseClass bc;
}
Comment
Add comment · Show 3
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 Ben 14 · Mar 02, 2011 at 07:04 PM 1
Share

From what you say, it should work. Can you post your three classes please?

avatar image Flynn · Mar 02, 2011 at 07:28 PM 0
Share

Posted the classes! :)

avatar image Peter G · Jul 19, 2011 at 12:59 AM 0
Share

Not sure why this question got bumped, but that's not really an anonymous type. That's just treating a subclass like a base class- Polymorphism.

1 Reply

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

Answer by Demigiant · Mar 02, 2011 at 07:35 PM

Your code seems ok, and I use subclassing all the time without issues.

The only thing that comes to my mind, is that maybe you're storing bc "as SubClass", instead than "as BaseClass", and thus the type don't match.

If not, could you post the method you use to store/instantiate bc?

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 Flynn · Mar 02, 2011 at 07:39 PM 0
Share

I do it all in Unity, non-runtime. The whole point of subclassing was so that all the subclassess could be treated as their superclass, thus making code allot simpler. I will try instantiating and adding the objects at runtime.

avatar image Flynn · Mar 02, 2011 at 07:44 PM 0
Share

It works! Thanks for pointing me in the right direction. I can't believe I didn't try that before posting. Sorry for the dud post :(

avatar image Demigiant · Mar 02, 2011 at 07:48 PM 0
Share

Lol, happy you got it working :)

avatar image Demigiant · Mar 02, 2011 at 07:51 PM 0
Share

P.S. I suppose you can still have it working via drag&drop in the editor, but you'll need a custom inspector to apply the correct typing.

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

1 Person is following this question.

avatar image

Related Questions

Creating an array of custom objects 1 Answer

Can I use lambda expressions or anonymous methods in JS? 1 Answer

Is Instance Of 3 Answers

How can I start fading out text upon setting it? 3 Answers

Accessing a private var from a subclass 2 Answers


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