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 Cbunz · Aug 29, 2018 at 11:02 PM · collider2dboxcollider2dgenericscapsulecollider

Is it possible to have a class's variable correspond to either a BoxCollider2D or CapsuleCollider2D in order to reuse the class for objects with either type of collider?

I have a class that I use on multiple objects. So far the objects have all had box colliders, but I've got one that now has a capsule collider. The variable for the collider is used throughout the class, so I'd prefer to be able to use the same variable whether it is a BoxCollider2D or CapsuleCollider2D. Is it possible to change the variable's type depending on which type of collider the object has?

I tried to use two separate variables of each type of collider (named colliderBox and colliderCapsule) that each GetComponent() of their collider types, then I checked to see which isn't null to determine if a bool (named hasBoxCollider) is true or false. Then throughout the class, I replaced the collider variable with:

(hasBoxCollider ? colliderBox : colliderCapsule)

Which doesn't work because "Type of conditional expression cannot be determined because there is no implicit conversion between UnityEngine.BoxCollider2D and UnityEngine.CapsuleCollider2D." So I've now learned that I must use variables of the same type or at least convertible between each other when using x ? y : z. Didn't know that until now.

I'm wondering if there is any possible way to go about this without a separate class or tons of if statements. Possibly with generics or custom colliders?

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
1

Answer by JVene · Aug 29, 2018 at 11:48 PM

Examine the documentation for Collider2D. Ask yourself if your requirements for the colliders fits entirely within the Collider2D interface. If it does, you can stop considering which collider is involved, and make the reference type a Collider2D instead of a BoxCollider2D, and be done.


If, however, that isn't sufficient, you have a few ways to think of this. You can fashion two scripts, one for Box and the other for Capsule, which are basically empty classes, but derive from your existing class refactored as a generic, where the generic parameter is the collider type you require. If you get that right, you're done.


It might turn out that while you require a few methods from Box when it's a box, and a few methods from Capsule when its a capsule, but for most of the code you only need the Collider2d methods, you might consider using a Collider2d reference overall, but just cast in those few cases where you must act based on which you require. You might fashion your existing class as an abstract base which then uses derivatives to override those few places with casts, leaving the rest of your code as is from a Collider2D perspective.

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

91 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider2D Resize Based on Sprite Using Generics 1 Answer

Turning off trigger with multiple box colliders? 0 Answers

How to move BoxCollider2D 1 Answer

How can I combine many BoxCollider2D into one Collider? 2 Answers

2D Colliders bug. 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