Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Alex2k17 · Aug 02, 2017 at 12:25 AM · prefabtaggameobjects

CompareTag to multiple GameObjects

I have multiple gameobjects that spawn randomly prefabs and I wanna do some actions when every gameobjects detects collision. I've tried with CompareTag but it didn't work.

     IEnumerator waitSpawner()
     {
         yield return new WaitForSeconds (startWait);
         while (!stop)
         {
             float spawnLocation=Random.Range(0,0);
             randEnemy = Random.Range (0, 2);
             Vector2 spawnPosition = new Vector2 (spawnLocation,transform.position.y);
             bulletclone=Instantiate (answers [randEnemy], spawnPosition, Quaternion.identity) as GameObject;
             yield return new WaitForSeconds (spawnWait);
         }
     }

This is from the first script when the prefabs are randomly spawn.

 void OnTriggerEnter2D(Collider2D col)
     {
         if(col.CompareTag("Correct"))
             {
                 test.text="Corect";
             }
         else if(col.CompareTag("Wrong"))
             {
                 test.text="Wrong";    
             }
     }

I don't get any error or warning, just doesn't work.

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

1 Reply

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

Answer by Yoshinator2 · Aug 02, 2017 at 12:33 AM

Your logic in the OnTriggerEnter2D is wrong. I actually have no idea what CompareTag does as I've never used it, but I do know how to do what you're trying to accomplish.

Instead of using Compare Tag, it is much cleaner if you simply just check to see if the collider's tag matches the tag you want, which I assume is what you are trying to get the script to do.

  void OnTriggerEnter2D(Collider2D other)
  {
      if(other.tag == "Correct")
          {
              test.text = "Correct";
          }
      else if(other.tag == "Wrong")
          {
              test.text = "Wrong";    
          }
  }

Please also note that it is common practice to use the variable 'other' for any collision/collider data in those Collision or Trigger Enter function. However, this is not required.

If this solved your problem, please accept my answer :)

Comment
Add comment · Show 5 · 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 Alex2k17 · Aug 02, 2017 at 12:44 AM 0
Share

It doesn't work, notice that the prefabs contains those tags.

avatar image Yoshinator2 Alex2k17 · Aug 02, 2017 at 01:00 AM 0
Share

So you're comparing tags on prefabs that haven't been spawned yet..?

avatar image Alex2k17 · Aug 02, 2017 at 01:05 AM 0
Share

It was my fault, I forgot to pot collider2D on prefabs. It works with other.tag== "Correct" and compare tag too. Thanks for info.

avatar image Bunny83 · Aug 02, 2017 at 01:30 AM 1
Share

Your answer is actually misleading. CompareTag is the preferred way to compare the tag of a gameobject as it doesn't allocate any memory. When you read the ".tag" property manually, Unity has to create a managed string which is returned by the property. CompareTag does perform the comparison on the native code side without the need to pass the object's tag value into the managed world.

avatar image Yoshinator2 Bunny83 · Aug 02, 2017 at 07:19 AM 0
Share

Wow ok, thanks for the info! I never knew that, I'll use that from now on. Thank you :)

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

84 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

Related Questions

Prebuilt gameobject bundles without Prefab instancing behavior. 0 Answers

how can I tag a gameobject as Player at the start of a scene loading from the last? 1 Answer

Checking Tag of Trigger Prefab 1 Answer

grab variable from an instance of a game object? 1 Answer

some prefab collisions randomly stop working 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