- Home /
Class hierarchy, gameObject, Raycast
Hi, i need know which parent class is a gameObject. eg:
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
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.
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.
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.
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?