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 Angry-Hobbit · Nov 30, 2012 at 08:55 AM · triggertag

How to TAG the Player?

Hi, Using the First Person Controller, I'm trying to create a simple box trigger that will play a sound 1 time when the player enters the box collider. I found several different scripts how to do this. However there are two things I don't understand...

***First, they all refer to "Tagging" the "Player". I have no clue what this involves.

***Second... all the scripts I find for player entering a box trigger and a sound playing are different. None seem to work and I just get errors. I don’t know if its from not "Tagging" the player (which I don’t know how to do ) or if the script is wrong.

Here is a sample of what I have been using.

function OnTriggerEnter(otherObj: Collider){ if (otherObj.tag == "Player"){ audio.Play(); } else { audio.Stop(); } }

Any help would be greatly appreciated. Thanks.

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
1
Best Answer

Answer by fafase · Nov 30, 2012 at 09:12 AM

1-To tag an object you have, look in the inspector at the top, you have Tag Untagged (probably).

2-If you click untagged it opens a drop down from which you can choose already existing tags. You can also add one at the end. It opens a new window in the inspector with a list of tags.

3-Open up Tag section with the little triangle and select the first empty element (probably the first one). Give a tag (Player) and press enter or it won't remain. Now get back to your object via the hierarchy and go back to 2 above.

Now add a script to the triggerbox:

 function OnTriggerEnter(other:Collider){
     if(other.gameObject.tag =="Player") audio.Play();
 }

Make sure your sound has Loop ticked off so it stops automatically. Now you want the sound only once if I got it right.

 var once:boolean = true;

 function OnTriggerEnter(other:Collider){
     if(other.gameObject.tag =="Player"){ 
         if(once){
            audio.Play();
            once = false;
         }
     }
 }
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 Angry-Hobbit · Dec 01, 2012 at 06:53 PM 0
Share

Ahh ... thank you so much for helping both in Unity and in how to properly use these forums. Yes I am a total NOOB to Unity. I am a 3D artist and not very "CODE" $$anonymous$$ded.

avatar image Angry-Hobbit · Dec 01, 2012 at 06:56 PM 0
Share

Also I wanted to let you know I am getting a compiler error when I use the script

function OnTriggerEnter(other:Collider){ if(other.gameObject.tag =="Player") audio.Play(); }

var once:boolean = true;

function OnTriggerEnter(other:Collider){ if(other.gameObject.tag =="Player"){ if(once){ audio.Play(); once = false; } } }

the error in the status window at the bottom of unity says "TriggerScript" already has a definition for 'OnTriggerEnter(UnityEngine.Collider)'

avatar image Angry-Hobbit · Dec 01, 2012 at 11:48 PM 0
Share

ahh the light bulb finally cam on. I see you ment those as 2 different scripts...I had them as 1 and that was my error. Thanks again...It works perfectly!

avatar image fafase · Dec 02, 2012 at 09:05 AM 0
Share

Hold on, nope. This is only one script. Fact is I show two steps. First is the simple one, second is the simple one evolved to play only once the sound. And your error is because you had twice the same function. So only use the second part that you attach to the object (not the player) with a trigger sphere or box collider. $$anonymous$$ake sure IsTrigger is ticked on the collider.

avatar image Angry-Hobbit · Dec 04, 2012 at 09:52 PM 0
Share

Ahh very good. Thanks again.

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

13 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

Related Questions

Do a collider only with certain taged objects 2 Answers

Check Trigger Collision's Name/Tag 1 Answer

Trigger not detecting tag 1 Answer

BCE0019: 'cont' is not a memBCE0019: 'cont' is not a member of 'UnityEngine.Component'. ber of 'UnityEngine.Component'. 1 Answer

How would I go about removing a mesh renderer component on collision with a trigger? 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