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
1
Question by fepunk59 · May 25, 2012 at 08:45 PM · ontriggerentertagifcondition

"tag" & "if condition with OnTriggerEnter

Hello
I have a collider who's many objects are going into it. For each object going to the collider, the score is +1.
Now, i want to have the score -1 for a special object.
I've tried to separate them with tag, but i have difficult. All object give me yet +1 .
Am I on the good direction? Do you have idea for doing that? Thank you (and sorry for my bad english)

Here my script:

 void OnTriggerEnter(Collider theCollision)
     {
     
     if (theCollision.tag == "piece")
     print("piece +1score");
     myPlayerScript.theScore++;
     
     if (theCollision.tag == "piecebonus")
     print("piece -1score");
     myPlayerScript.theScore--;
     
     }
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
6
Best Answer

Answer by You! · May 25, 2012 at 09:12 PM

Firstly, there are a few fixes to your code...they may help.

 void OnTriggerEnter( Collider theCollision ) // C#, type first, name in second
 {    
     if (theCollision.gameObject.tag == "piece")
     // By using {}, the condition apply to that entire scope, instead of the next line.
     { 
         print("piece +1score");
         myPlayerScript.theScore++;
     }
     
     else if (theCollision.gameObject.tag == "piecebonus")
     {
         print("piece -1score");
         myPlayerScript.theScore--;
     }    
 }

If these fixes aren't enough, you might have to change "theCollision.gameObject.tag" to "theCollision.gameObject.name" and use the object's name instead. Also check to see if you have the object tags correct in your script (this last problem is one I've had before).

[Edit by Berenger : Collider theCollision instead of theCollision : Collider (C#), explained the why {}]

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 fepunk59 · May 25, 2012 at 09:54 PM 0
Share

I tried your fixes but it don't work. All the conditions are executed and tag or name is not check

$$anonymous$$aybe must i search an other way to do that

avatar image You! · May 25, 2012 at 10:00 PM 0
Share

If you use "theCollision.gameObject.name" you must use the name of the object...if that is your problem...

I just saw that there aren't nearly enough curly braces. I will update my answer to show where you need them!

Edit: You should probably also use "else if" on your second "if" statement, just because both statements are calling for similar stuff... This won't really change how it operates, though.

avatar image fepunk59 · May 26, 2012 at 10:23 AM 0
Share

Thank you for your help

$$anonymous$$y tag was on the instantiate prefab but not in the mesh. Now all work perfect

Thanks to you, my script is cleaner

avatar image You! · May 26, 2012 at 03:08 PM 0
Share

thanks :) !

avatar image ganyotcu · Jun 19, 2017 at 08:56 PM 0
Share

thanks master64

avatar image
0

Answer by Berenger · May 26, 2012 at 02:46 AM

Using the name for that purpose is ill advised, as you might need it for something else. Several object with different names can share the same tag however.

If the score does go up no matter what object collide with that one, it means none of them has the tag piecebonus. Make sure it's correctly set in the editor and that it's not modified by a script somewhere.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject tag to if condition 1 Answer

Check if there is a child with a tag? (multiple children but each diff tag) 1 Answer

random range/if conditions? 2 Answers

if for array 1 Answer

OnTriggerEnter doesn't read a tag of a moving 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