Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by User12345 · Mar 23, 2011 at 11:41 AM · stringtagcompare

Tag comparision with a string as variable doesnt work

very simple: I get the other as a string and if that string is equal to the tag of the player it should do smoething

private void TakeDamage(string other){

 Debug.Log(Player.tag + " " +other);

 if(Player.tag == other){

     Debug.Log("attack player");
 }

}

The Debug.Log shows that both strings (tag and other) er exactly the same. But it just doesnt work. I tried it with Player.CompareTag and got the same result.

this is the Debug.Log:

tps_player tps_player (UnityEngine.GameObject) UnityEngine.Debug:Log(Object)

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

4 Replies

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

Answer by yoyo · Mar 23, 2011 at 03:13 PM

The existence of the CompareTag method suggests that tag strings may be stored in a way that doesn't work properly for normal string comparison.

Try this:

if(Player.CompareTag(other))
...

If that doesn't work, you could inspect the bytes of each string with a method like this:

void LogStringChars(string s)
{
    string msg = s + "=(";
    foreach (char c in s)
    {
        msg += (int)c + ",";
    }
    msg += ")";
    Debug.Log(msg);
}

Just add the following to your method ...

LogStringChars(Player.tag); 
LogStringChars(other);
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 User12345 · Mar 23, 2011 at 04:24 PM 0
Share

well, that was my first attempt. but as I said it doesnt work with compareTag either. I get this error message when using comparetag

UnityException: Tag: tps_player (UnityEngine.GameObject) is not defined! EnemyAI.TakeDamage (System.String other) (at Assets/...

avatar image yoyo · Mar 23, 2011 at 09:19 PM 0
Share

Sorry, obviously I read too quickly and missed that you'd already tried that. I edited my answer with another suggestion. Weird problem, good luck!

avatar image User12345 · Mar 23, 2011 at 11:53 PM 0
Share

Thanks a lot ill try that!

avatar image
0

Answer by Bunny83 · Mar 23, 2011 at 12:12 PM

The tag of a GameObject is just a string, so that comparison will work if they are equal. since it doesn't work they are not equal. Maybe a space at the end?

ps. what does tps stand for? you don't wan't to write fps?

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 User12345 · Mar 23, 2011 at 12:39 PM 0
Share

There are no spaces or what ever. and the debug shows that they should be exact the same. the t stands for THIRDperson ;)

avatar image
0

Answer by User12345 · Mar 24, 2011 at 12:25 AM

Ok I found another way... or... I probably found the main problem.

the "other" string I sent was actually the name of a gameobject (I used gameObject.ToString(); to convert)that had the same name as the tag! Somehow unity makes a difference between strings and their origin.

So what I did now was to send the gameobject.tag instead of the gameobject.ToString(); which would have been the most logical in the first place... dont know what I was thinking there -.-

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 Mike 3 · Mar 24, 2011 at 12:27 AM 0
Share

Even scarier - ToString adds on the type. If you want the name of a gameobject, use go.name

avatar image
0

Answer by Rafes · Jul 30, 2011 at 05:54 PM

If you want to know if something is a specific game object, you can test if the gameObject is equal to the other gameObject.

Tags are good for testing if the hit item is part of a group of things.

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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Define a string variable as a tag? (C#) 1 Answer

How do I find a String in an Array? 2 Answers

Is there something like Animator.StringToHash() but for tags? 0 Answers

Tag is not defined, when it has been manually created? 1 Answer

Compare two time strings? 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