• 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
Question by UnityTeddy · Feb 06, 2013 at 09:29 PM · collisioncollidertagfindwithtag

Tags returning true before collision

Hi! So, just a simple and quick question that I can't find an answer to. And yes I've tried searching the answers and the forums but didn't find anything related.

When I'm using tags to check if a enemy is colliding with a waypoint, and also when I try to check if my player is colliding with a wall, the if-statement returns true even if there's no collision with a collider with that tag. Even if the game object with the tag is more than 3 units away, and thus not colliding with the other object, the statement still returns true.

So I guess my question(s) is:

What could be causing this?

Is anyone else experiencing this problem?

Is there a solution to this?

Thanks in advance!

Erik

Edit:

For those of you who wanted the code:

Player code:

     function OnCollisionStay (col : Collision)
     {
         if(col.gameObject.FindWithTag("Terrain"))
         {
             canJump = true;
         }
         
         else
         {
             canJump = false;
         }
     }

Enemy code:

   function OnCollisionEnter (col : Collision)
     {
         if (col.gameObject.FindWithTag ("Waypoint"))
         {
     //code for switching walking direction
         }
     }

And yes, both the player and the enemy are colliding with something else, the ground ("Terrain"). BUT, as they both are colliding with the terrain as soon as I start the game and the statement doesn't return true until a certain position in the game, I don't think that this is the issue. If it was, then the statement would return true as soon as I started the game (i.e. as soon as they hit the ground).

Comment

People who like this

0 Show 3
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 Screenhog · Feb 07, 2013 at 11:16 PM 0
Share

Could it be colliding with something else instead?

avatar image youngapprentice · Feb 07, 2013 at 11:37 PM 0
Share

Could you post up the code? and are you using

if( myTarget.transform.tag == "myString" ?

avatar image UnityTeddy · Feb 09, 2013 at 12:52 PM 0
Share

Posted the code, if you're still interested.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by WelRC · Feb 09, 2013 at 01:30 PM

If you want to check the collider tag, try CompareTag: col.gameObject.CompareTag("YourTag")

Looking at FindWithTag description:

static function FindWithTag (tag : String) : GameObject Description Returns one active GameObject tagged tag. Returns null if no GameObject was found.

So your code was returning any object with that tag, disregard of collision. This is because the gameObject attached to collider inherits the GameObject member functions, including FindWithTag.

Comment

People who like this

0 Show 0 · 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

Answer by Ghopper21 · Feb 09, 2013 at 01:29 PM

The expression

 col.gameObject.FindWithTag("Tagname")

is not doing what you think it's doing. If you look in Unity's script reference for GameObject, you'll see that FindWithTag is a "class function," meaning it isn't specific to any specific game object; instead it finds any object in your scene with that tag.

In order words, it's equivalent to calling the function via the class (i.e. GameObject.FindWithTag("Tagname"), without any specific instance.

What you want is:

 col.gameObject.tag == "Tagname"

i.e. checking the tag of the specific game object you collided with.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

A limit to the number of tags? 2 Answers

"Player" tag collider from other GameObject? 1 Answer

plain problem with collider. begginer in unity 3 Answers

My script says object is not colliding with the tag! But it is! 1 Answer

Collision Issue 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