Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Levantez · Feb 20, 2014 at 04:03 PM · class objectclass instance

How do I check if a derived class is a certain class?

For example,

I have ScriptableObject A and then 3derived class A1 A2 A3

There is also Runtime Class B and derived from it, B1 and B2

B1 can only use A1 and A3 B2 can only use A2

And finally, there is manager Class C

Class C can only call function from base class B, But the said function can received any version of classA

As such, class B1 and B2 have a need to check which kind of A they are receiving.

public class B1 { void Update() { if( A is not A1 or A2 ) return; }

}


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 CodeElemental · Feb 20, 2014 at 08:43 PM 0
Share

Did my answer resolve your issue? If not, can you explain what is the error/what is missing? If yes mark it as accepted answer.

1 Reply

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

Answer by CodeElemental · Feb 20, 2014 at 04:05 PM

 if (objA is ClassA)

More here

In your specific scenario :

 public class B1 { 
    void Update() 
    { 
       if(!(A is A1 || A is A2)) 
          return;
 
    }
 }
Comment
Add comment · Show 2 · 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 Gargosian · Dec 22, 2020 at 12:15 PM 0
Share

Helped a lot! Is there also a way to then call a method which only exists on A2 for example?

avatar image Bunny83 Gargosian · Dec 22, 2020 at 02:30 PM 1
Share

Of course there is a way ^^. If you use an older version of Unity you would either use an "as" cast in combination with a null check or an "is" check followed by a normal cast. However in newer Unity versions where C#7+ featues are supported you can use an is operator with integrated variable declaration- So you can do this:

 if (myVar is $$anonymous$$yType obj)
 {
     // use the variable "obj" here
 }

edit
Just for completeness, here are the other two cases I mentioned above

 // is operator in combination with normal cast.
 if (myVar is $$anonymous$$yType)
 {
     $$anonymous$$yType obj = ($$anonymous$$yType)myVar;
     // use the variable "obj" here
 }

 // as operator with null check
 $$anonymous$$yType obj = myVar as $$anonymous$$yType;
 if (obj != null)
 {
     // use the variable "obj" here
 }

The "as" cast is slightly more expensive than a normal cast but usually cheaper than an "is" check and a normal cast combined. The as cast will not generate any error or exception if the cast fails but simply returns null if the cast is not possible. The normal cast will throw an invalid cast excpetion if the cast is not valid.


The new "is" operator is now the shortest and cleanest solution in most cases. The "as" cast variant is useful if your source variable could also be null as this would be checked implicitly with the null check.

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

[CLOSED] How do I check if a derived class is a certain class? 0 Answers

How do I check if a derived class is a certain class? 0 Answers

C# finding gameobject that created this class, and do I need to clear them? 1 Answer

How to create a repository of enemy objects that are initialised but then get a new instance of a random one? 1 Answer

Every active Object a Script? no some efficient way? 4 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