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
2
Question by Bennett-Lynch · Oct 13, 2014 at 03:06 AM · c#collidertype

Checking "type" of "Collider" in C#

I have objects of type "Creep" that are colliding into a trigger to be destroyed. At first, I assigned them a tag and used this conditional (which does work):

 void OnTriggerEnter(Collider other) {
     if (other.gameObject.tag == "Creep")

But I've decided I'd rather check for the type of the object colliding so I don't have to tag all future objects. I've been searching around and tried all of the following methods, none of which have worked:

 if (other.GetType().ToString() == "Creep")
 if (other.GetType() == Creep)
 if (other is Creep)
 if (other.GetType() == typeof(Creep))

Is there another way to do this that I'm not aware of? Or should one of these work and perhaps something else is causing the trouble?

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

3 Replies

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

Answer by Landern · Oct 13, 2014 at 03:14 AM

other is of Collider type.

The other.GameObject varoab;e is a GameObject type with a component of "Creep" on it. You will need to use GetComponent, otherwise it's a GameObject that ultimately derives at the bottom from UnityEngine.Object.

You will take a hit using GetComponent, but only you know how much it's called and whether it will be a real performance hit.

Comment
Add comment · Show 3 · 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 Kiwasi · Oct 13, 2014 at 03:19 AM 1
Share

Translation

 if(other.gameObject.GetComponent<Creep>())
avatar image Landern · Oct 13, 2014 at 03:37 AM 1
Share

@Bored$$anonymous$$ormon +1, yes, simply put.

And just in case there is any wonder why no operator is specified in the if statement.

The internal objects(Technically UnityEngine.Object if memory serves) from Unity overload the operator "==" and method .Equals(), this is why you can put an object that derives/inherits from $$anonymous$$onoBehaviour as the condition of an if statement and it will return true (the object is not null) or false (the object is null). Some c# programmers do this on their base classes in order to save some typing checking for null.

avatar image Bennett-Lynch · Oct 13, 2014 at 03:06 PM 0
Share

Thanks, guys. How much of a performance hit are we talking? If I call it once every second will it be negligible, or would it be worth going the tag route even in that scenario?

avatar image
1

Answer by Bennett-Lynch · Oct 14, 2014 at 08:01 PM

This is the code I ended up using (for anyone who stumbles upon this later down the road):

 if (other.gameObject.GetComponent<Creep>())

It checks to see if the object has a component of type Creep (i.e., it has a Creep.cs script attached).

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

Answer by conceptfac · Aug 27, 2020 at 02:50 PM

How to check local collider not other???

Comment
Add comment · Show 1 · 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 Llama_w_2Ls · Aug 27, 2020 at 03:10 PM 0
Share

Try using OnCollisionEnter for local colliders

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

BoxCollider Overlap OnTriggerEnter Problem 1 Answer

Drag a sprite by its 2D Polygon Collider component 1 Answer

C# Slow Down all Gameobjects Within another Gameobject's Collider 2 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