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 chetanisinanand · Mar 25, 2015 at 09:57 AM · c#classinstances

A Variable that can hold different class Objects

I've 2 classes :

 [System.Serializable] public class A  {
     [SerializeField]    public bool apply;
     [SerializeField]    public float pointInSpeedDuration ;
     [SerializeField]    public float pointIntervalbetweenSpeedUp ;
     [SerializeField]    public float variablePointSpeed ;
 }
 
 [System.Serializable] public class B{
     [SerializeField]    public bool apply;
     [SerializeField]    public float reverseDuration ;
 }


Somewhere in a third class I'm creating object of these classes :

 public class LevelController : MonoBehaviour {
     public int levelIndex ;
     public A objA = new A();
     public B objB = new B();
     
 }

However I need only one class(A or B) at a time ,

please let me know if I can do something so that I can have a single variable which can hold class A or B:

 public class LevelController : MonoBehaviour {
     public int levelIndex ;
     public var obj = new A();
 //OR
     public var obj = new B();
 }

for me, "var" is not working in the above example

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
0

Answer by ParanoidSnail · Mar 25, 2015 at 10:12 AM

What you are looking for is called Inheritance. You can find a lot of information about this on google.

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/inheritance http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29

Comment
Add comment · Show 3 · 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 chetanisinanand · Mar 25, 2015 at 10:57 AM 0
Share

I'm not very good in C# concepts, however if I create a class C that inherits from Class A works well but how to inherit it from class B at same time , can I change inheritance at runtime ?

[System.Serializable] public class C: class A{

}

avatar image fafase · Mar 25, 2015 at 11:50 AM 0
Share

you want a class A with any common things, in your case the apply variable. Then you have B and C that inherit from A and as they inherit, they are A.

In your other script you have

   public A myInstance;

now since B and C are A (inheritance) they can fit in that spot. Via that variable only what is in A is accessible. SO you would have to cast to access lower level variables or look into polymorphism (roooh that surely is a fancy name ). It would be a little long to explain so google is your friend.

avatar image chetanisinanand · Mar 25, 2015 at 04:39 PM 0
Share

Thanks fafase for your suggestion, polymorphism is ok , however my problem is the non-$$anonymous$$onoBehaviour derived class are not serializable As mentioned somewhere , I'm trying to achieve this , however its not visible in inspector :

  public class Base {...}
  
 public class Triangle : Base {...}
 public class Square : Base {...}
 public class Circle : Base {...}
  
 public class $$anonymous$$yBigClass {
     public Base m_i_am_polymorphic; // this can be assigned a Triangle, Square, Circle, or anything that inherits from Base
 }

 

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instancing class automatically? 2 Answers

All Arrayinstances get changed -1 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getting NullReferenceException when creating class instance using List 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