• 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 Madswint · Apr 18, 2014 at 11:41 PM · audiotriggermusic

Music Zones play music with tags?

I'm trying to make music zones, enter a music zone and it will play music according to the area. I want it all in one script. I figured out how to do it with tags, but I think I'm doing it terribly wrong :(. ALSO! It's supposed to play the music in the main camera. How can I do that?

 #pragma strict
 
 var MusicZone1Adventure : AudioClip;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnTriggerEnter() {
 if (gameObject.tag == "MusicZone1Adventure") {
 audio.PlayOneShot = MusicZone1Adventure;
 }
 
 }

Can anyone point out what I'm doing wrong? Thanks in advance!

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 T-D-M-3 · Apr 19, 2014 at 12:19 AM

The way that I usually do things like that i use this:

 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnTriggerEnter(hit:Collider){
 if(hit.tag == "Player"){
 //Whatever you need to do
 }
 }

Basically it activates the code as soon as anything with the Player tag hits it. so you could rig it to play the sound when the player reaches a certain point.

Comment
Add comment · Show 8 · 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 Madswint · Apr 19, 2014 at 12:25 AM 0
Share

Yes, but that would make me have to make a new script for every music trigger. I'm trying to make it so all triggers have tags with their music name on it. When player goes into a trigger, the music according to the trigger's tag will play. Any suggestions towards that?

avatar image T-D-M-3 · Apr 19, 2014 at 09:05 PM 1
Share

Okay, now i see what you mean. so what you need to do is this:

 var Clip1 : AudioClip;
 var Clip2 : AudioClip;
 var Clip3 : AudioClip;
 var Clip4 : AudioClip;
 var Clip5 : AudioClip;
 
 function OnTriggerEnter(hit:Collider){
 if(hit.tag == "Clip1"){
 audio.Stop();
 audio.PlayOneShot(Clip1, 0.7);
 }
 if(hit.tag == "Clip2"){
 audio.Stop();
 audio.PlayOneShot(Clip2, 0.7);
 }
 if(hit.tag == "Clip3"){
 audio.Stop();
 audio.PlayOneShot(Clip3, 0.7);
 }
 if(hit.tag == "Clip4"){
 audio.Stop();
 audio.PlayOneShot(Clip4, 0.7);
 }
 if(hit.tag == "Clip5"){
 audio.Stop();
 audio.PlayOneShot(Clip5, 0.7);
 }
 }
 
 @script RequireComponent(AudioSource)
 

just attach this script to the camera and when the player hits anything tagged with Clip1, clip2... it will play the corresponding sound. hope this helps!

Quick edit it needs to be attached to the Player object.

avatar image Madswint · Apr 19, 2014 at 09:45 PM 0
Share

It's not working :<

avatar image T-D-M-3 · Apr 19, 2014 at 10:56 PM 0
Share

Do you have it on the player Object

avatar image Madswint · Apr 19, 2014 at 10:58 PM 0
Share

Yes it's as if it doesn't react to the trigger at all

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Music Zones play music with tags? 0 Answers

Stream music in Unity mobile 0 Answers

Audio Trigger 3 Answers

Audio Source will not "unmute" after toggling? 2 Answers

Unintended audio gaps 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