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 DMCH · Dec 16, 2012 at 06:44 PM · gameobjecttype

How to get the type of a Gameobject

Hello,

The question in its simplest form: I have a game object. How do I check if what it's type is? If it's a prefab called "Combat Unit", how do I check this? I've tried other.getType().equals("Combat Unit") but this doesn't work.

Thanks for your help!

Read on if you'd like some context!

About the game: 2D / side view. Units march across the screen, and stop to fight any enemy units or structures they encounter. Units and buildings have a rigidbody.

The problem: I'd like to implement different behaviour depending on whether the gameobject the unit encounters is an enemy unit( created from a prefab called "Combat Unit") or a building (gameobject that is automatically present in scene). Particularly, I need units to collide, and units to halt a distance away from buildings.

What I've tried so far: I've added a child gameobject with a box collider to the unit to determine if it is close to another object with a rigidbody. Iget the scripts attached to the collision game objects. Since the scripts are different, i can check whether each is null to determine the type of the unit in the collision. This works, but I don't think it's good practice, and could possibly cause problems down the line as the code gets more complex.

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
4
Best Answer

Answer by Eric5h5 · Dec 16, 2012 at 07:04 PM

Since a GameObject is by definition a GameObject, the type can't be anything other than GameObject. Use tags and CompareTag.

Comment
Add comment · Show 7 · 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 fafase · Dec 16, 2012 at 07:06 PM 0
Share

From the look of the question,by type I think he means name.

 If it's a prefab called "Combat Unit" 
avatar image Eric5h5 · Dec 16, 2012 at 07:16 PM 0
Share

Yeah, he's wanting a "type" for different prefabs, so I explained why GetType wouldn't work for this, since that's not actually the kind of type that applies here. The name would technically work and is a valid answer, but I think tags are more appropriate here, since they are meant to apply to categories of objects, whereas the name is more for individual objects. Also you have the issue where instantiating a prefab will cause it to have "(Clone)" added to the name by default, which can confuse things, so I think tags are cleaner for this particular usage. (Also you get to use CompareTag, which is a little more efficient than string comparisons.)

avatar image fafase · Dec 16, 2012 at 07:18 PM 0
Share

Yep I was about to think of the clone thing. Tag would be the best alternative.

avatar image DMCH · Dec 16, 2012 at 10:33 PM 0
Share

Thanks for the help all!

avatar image yoyo · Dec 17, 2012 at 04:48 AM 1
Share

You could also use gameObject.GetComponent() -- assu$$anonymous$$g there is a component script of type CombatUnit that it makes sense to check for. (Tags are good too, sometimes testing for a component makes sense.)

Show more comments
avatar image
2

Answer by Jeff_B · May 31, 2015 at 11:43 AM

"Since a GameObject is by definition a GameObject, the type can't be anything other than GameObject."

Oddly, that is actually helpful and gets a +1 from me -- I needed the repetition to help with an issue of "what type of object am I dealing with"... well, now it's obvious: it's a GameObject, of course! (It's turtles all the way down...)

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
2

Answer by Raistlin2015 · Mar 30, 2017 at 04:04 PM

I know I am WAAAAY late to answer DMCH but for those who have this question in the future. If for instance you want to detect the Type of an object that another object has interacted with you could do something like the following.

     private void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.GetComponent<Character>())
         {
             Debug.Log("Character Hit");
         }
     }

This has worked for me. I'm not sure how this affects performance but I like this over using tags so that an object can be checked for more than one interaction. Only one tag can be put on a gameObject. For example if I want to check if the Character is an NPC in one instance and in another I want to check for all objects that inherit from Character, well then a tag wouldn't work.

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 unity_26EBDAE273C6BC4B3EF1 · May 31 at 07:06 PM 0
Share

personally I couldn't make it work with this answer, but with a similar idea using tags (so you should add a tag "player" to your player obviously)

if(col.gameObject.CompareTag("Player")))

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

15 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

Related Questions

Determine if game object is tyoepf One Prefab 3 Answers

Need help with typing game 1 Answer

What is the type of my prefab? 2 Answers

Class hierarchy, gameObject, Raycast 1 Answer

Creating a Script at runtime, and adding it to an object... 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