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 dario.gmdtech · Jan 21, 2015 at 02:04 AM · gameobjectraycastparentclasstype

Class hierarchy, gameObject, Raycast

Hi, i need know which parent class is a gameObject. eg:

alt text

I have 4 game objects: circle, square, cube and sphere (with boxCollider each one) Making

 RaycastHit hit;
 Physics.Raycast(ray,out hit,10f);

how i can know if hit.collider is a shape? (hit.collider is kind of class: shape?) how i can know if hit.collider is a 2D class type? how i can know if hit.collider is a cube class type? (without "(clone)" string).

Thank you. Darío

classhierarchy.jpg (11.0 kB)
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
Best Answer

Answer by Simon-Larsen · Jan 21, 2015 at 02:14 AM

You could either tag your object (top left in your inspector) and check their tags like so:

 if (hit.gameObject.tag.Equals("MyShapeTag"));

Or you could ensure that each different shape has a unique component, which you can try to get.

 if (hit.GetComponent<BoxCollider2D>() != null); // True if the object has a BoxCollider2D component

You could also write you own script and attach it to each different shape and send a message to that particular object. This way they can handle the event themselves.

 hit.SendMessage("RaycastHit", 5.0F);

My conclusion would probably be to use the tags to evaluate your objects. You could read the documentation on this here.

EDIT Didn't realize you were actually already using inheritance, but in that case you can indeed use polymorphism. What you need to do is to use GetComponent() to fetch your script, but you should search for the parent class of your hierarchy. To continue your example where you have a class Shape which is inherited by Circle, you could check if a shape is a circle like so:

 Shape shape = hit.collider.GetComponent<Shape>();
 if (shape != null && shape is Circle)
 {
     // A Circle script is attached to the gameObject.
 }

You could also check if (shape is 2D), like you ask in the comment, since the 2D class also inherits from the Shape class.

Comment
Add comment · Show 5 · 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 dario.gmdtech · Jan 22, 2015 at 01:54 AM 0
Share

hi, thank you, but i need some like this

hit.collider.gameObject.GetType().IsSubclassOf(typeof(Shape)) and hit.collider.gameObject.GetType().IsSubclassOf(typeof(3D))

because some objects have shared methods name.

avatar image Simon-Larsen · Jan 22, 2015 at 02:33 AM 0
Share

If you want to call methods of the same name between multiple clases you should look into inheritance and polymorphism. That is if you haven't already. Do you have a Shape class which is inherited from? In that case you can

 GetComponent<Shape>();

and from there either call a common method or check if (shape is Circle) and do something specific.

avatar image richyrich · Jan 22, 2015 at 02:38 AM 0
Share

@Simon Larsen see picture in the question ;)

avatar image dario.gmdtech · Jan 22, 2015 at 03:39 AM 0
Share

look, this, I have class like:

 public class shape : $$anonymous$$onoBehaviour {
         public string getName() ...
 }
 
 public class 2D : shape {
         public float getArea()...
 }
 
 public class 3D : shape {
         public float getVol()...
 }
 
 

and with the raycast i want to do:

 RaycastHit hit;
 Physics.Raycast(ray,out hit,10f);
 if(hit.collider.gameObject.GetType().IsSubclassOf(typeof(2D)) ){
        debug.Log(hit.collider.GetComponent<2D>().getArea());
 }
 
 if(hit.collider.gameObject.GetType().IsSubclassOf(typeof(3D)) {
        debug.Log(hit.collider.GetComponent<3D>().getVol());
 }
 
 if(hit.collider.gameObject.GetType().IsSubclassOf(typeof(Shape)){
        debug.Log(hit.collider.GetComponent<Shape>().getName());
 }


But this dont work, how i can do it?

avatar image Simon-Larsen · Jan 22, 2015 at 07:35 PM 0
Share

Edited the answer.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Accessing a GameObject parent Object 2 Answers

How to make a Ray class? 1 Answer

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 1 Answer

Display Name Above Object 2 Answers

GameObject facing hit.point doesn't always work 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