• 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
0
Question by gev-alex · Nov 26, 2020 at 06:28 PM · tagscompare

CompareTag doesn't work

Hi everyone, i wrote a code, it detect colliders and add in list "side", but then i trying to compare it with tag, i always get false, could someine give an advice?
Collider[] side; bool Obstacle; ..... void Update { if (Attached)
{ side = Physics.OverlapSphere(transform.position, 0.5f); foreach (var x in side) { Obstacle = x.CompareTag("Obstacle") ? true : false; } } }

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

2 Replies

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

Answer by Envans · Nov 26, 2020 at 06:51 PM

Check that object have the tag Obstacle, also spelling and case matters what I mean is obstacle != Obstacle that tag name must be the same for the objects as in the script you wrote.

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 gev-alex · Nov 26, 2020 at 06:53 PM 0
Share

Already, it same

avatar image Envans · Nov 26, 2020 at 06:57 PM 2
Share

Ok I didnt notice something, in your loop you check every collider hit so if you hit some obstacles but also something else and that something else is "later" in the list than the obstacle then the variable is set to false change your foreach loop code to this

Obstacle = false; foreach (var x in side) { if(x.CompareTag("Obstacle"){ Obstacle = true; return;} }

avatar image gev-alex Envans · Nov 26, 2020 at 07:04 PM 1
Share

Works perfectly, thx))

avatar image
0

Answer by S-khan · Nov 26, 2020 at 07:02 PM

Debug your foreach loop, check you're getting your desired GameObjects. Also, check if you're getting any child that hasn't tagged yet. For debugging, put a Breakpoint on your foreach loop line and Attach to Unity. Make sure you're using vs2019.

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Are tag settings passed down from Game Object parent to mesh child? 1 Answer

Is it possible to have my character latch to an object? 1 Answer

Creating tags via code 3 Answers

Deleting gameobject with tag problem 1 Answer

GameObject.FindGameObjectsWithTag problems 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges