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 JesusChristChangedMe · Nov 09, 2010 at 10:09 PM · audioclipswitchaudioplay

Audio Play/Stop Trouble

Hi, Im having trouble with my sound on my game. i have it where when you hit an invisible wall then music starts to play. but i want music playing before that to stop playing and my new music to start. so basically i need one audio clip to stop and a different one to start at the same time. Thanks for any help!

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

Answer by Jesus_Freak · Nov 09, 2010 at 10:29 PM

here's a script from the unity scripting reference. it stops an already playing sound or music file after it's fully done playing, pauses it, then plays a sound file.

// Play default sound audio.Play();

// Wait for the audio to have finished yield WaitForSeconds (audio.clip.length); audio.Pause();

// When the audio component has stopped playing, play otherClip var otherClip : AudioClip;

function Update() { if(!audio.isPlaying) { audio.clip = otherClip; audio.Play(); } }

you could modify it so that it plays and stops the sound when you hit that wall:

var Sound2 : AudioClip;
function OnTriggerEnter(hit : collider)
{
 if(hit.gameObject.tag == "PlayStop")
 {
  audio.pause;
  audio.clip = Sound2;
  audio.Play(Sound2);
 }
}

that last one might not work, but i know the top one works perfectly. and don't forget to tag your wall "PlayStop" !!!!

okay. to pause an already playing sound, just do

var Sound1 : AudioClip; var Sound2 : AudioClip;

function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "PlayStop") { audio.Pause(Sound1); audio.Play(Sound2); } }

Comment
Add comment · Show 2 · 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 Jesus_Freak · Nov 09, 2010 at 10:32 PM 0
Share

and actually, i just read, you could just add a pause statement to the original sound. i'll add that to my answer. (5:32)

avatar image JesusChristChangedMe · Dec 11, 2010 at 01:06 PM 0
Share

Thanks for the help!

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

No one has followed this question yet.

Related Questions

Stop audio from looping and play at lower volume 1 Answer

How to create an audio manager script with input from triggers? 2 Answers

Playing audio brings this error: An invalid seek position was passed to this function. Does anyone knows what does this mean? 1 Answer

One sound gets quieter when other is played? 1 Answer

How Do I Get This To Play Sound? 0 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