- Home /
Set a variable to a collider type.
I'm trying to set a variable to a collider type.
When a particle hits an enemy I want it to see what type of collider it has just hit, box, capsule or sphere .
I thought something like this would work
private var col : Collider;
private var colCenter : Vector3;
col = _enemy[i].Transforms.gameObject.GetComponent.<Collider>();
var colType = col.GetType();
colCenter = _enemy[i].Transforms.position + colType.center;
But I get the error .. 'center' is not a member of 'System.Type'
Also tried
private var colCenter : Vector3;
var col = _enemy[i].Transforms.gameObject.GetComponent.<Collider>().GetType();
colCenter = _enemy[i].Transforms.position + col.center;
But still get the error .. 'centre' is not a member of 'System.Type'
Any help please, thanks.
Answer by FortisVenaliter · May 13, 2015 at 04:23 PM
How about something like this:
if(center is BoxCollider)
{
BoxCollider box = (BoxCollider)center;
// do code with box
}
else if(center is SphereCollider)
{
...
}
else ...
Thanks for the input, but I really didn't want to go that route .. trying to keep it optimised :)
There shouldn't be any significant performance decrease for that code, unless you're calling it over 100$$anonymous$$ times per frame or something.
You can't write a single bit of code to handle them individually unless you can do so with the abstract base class. Since you need to deter$$anonymous$$e the type though, I'm assu$$anonymous$$g that's not possible in your situation?
The GetType() method returns a System.Type object. It basically gives you metadata about the type of object from Reflection, but has no actual link to the object in question.
One other thing I have to recommend is to not use "var" in C#. I personally treat it like it doesn't exist, because strong typing makes your code easier to read and debug (in almost all cases anyway).
FortisVenaliter .. thanks for that, looks like it's not possible then :(
Seriously though, don't pre-guess on performance. Give it a shot and profile it, then edit if it's too slow.
I did put a smiley face when I said "trying to keep it optimised" ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613195519im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My SphereCast dont collide with Box collider 1 Answer
Colliders not inheriting the Transforms of changed animation states(Run to Crawl) -Unity3D, 0 Answers
Raycast, Linecast, sphere collider or capsule collider? 0 Answers
Keep Sphere Collider within Capsule Collider 0 Answers
Capsule collider bounces where two ground colliders meet 2 Answers