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 Biendeo · Jun 22, 2010 at 08:34 AM · audiovariableclipaudioplay

How do I play a sound during a level change?

Earlier, I asked the question how to end a level. It was solved with the following script:

//Script to revert winning player back to main menu

var levelToLoad : String;

function OnTriggerEnter(hit : Collider) {

Application.LoadLevel(levelToLoad);

}//END FUNCTION ONTRIGGERENTER

Now, I want to add a sound to it. What I've attempted doing is creating a variable called triumph, and then using the audio.PlayOneShot code. However, this never plays it, and actually prevents the player from moving to the next level. I'm not sure how to add the sound to it. The sound is named Pop! Triumph! and it doesn't loop. Also, I'm not going to use any other sound, so it doesn't have to be a variable.

So can anyone make an addition to this script to make the sound play?

Comment
Add comment · Show 1
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 duck ♦♦ · Jun 22, 2010 at 11:08 AM 0
Share

Do you have any errors in your console? if it never loads the level, it sounds like it's causing an error and ceasing execution of that function.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AnaRhisT · Jun 22, 2010 at 08:50 AM

  1. Import your audio files into your Unity Project. These are now Audio Clips.
  2. Go to GameObject->Create Empty from the menubar.
  3. With the new GameObject selected, select Component->Audio->Audio Source.
  4. Assign the Audio Clip property of the Audio Source Component in the Inspector.

Hints:

* The key to a nice sound environment is tweaking the Rolloff Factor.
* 3D audio effects will only work for mono Audio Clips. Stereo Audio Clips will be mixed as-is into the sound output. 

I'll give u a C# verison, the sound file has to be in ur "Resources" Folder.

using UnityEngine; using System.Collections;

public class Sound_Player: MonoBehaviour { AudioSource audio_source; AudioClip sound; // Use this for initialization void Start () { audio_source = (AudioSource)gameObject.AddComponent("AudioSource"); sound = (AudioClip)Resources.Load("File Name"); audio_source.clip = sound; } // Update is called once per frame void Update () {

 }


//Script to revert winning player back to main menu

var levelToLoad : String;

function OnTriggerEnter(hit : Collider) {

audio_source.Play(); Application.LoadLevel(levelToLoad);

}//END FUNCTION ONTRIGGERENTER

}

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
avatar image
1

Answer by Mike 3 · Jun 22, 2010 at 01:15 PM

The problem is that the sound will be stopped when you try to load the next level

What I would do is play a sound (using audio.Play, not PlayOneShot), and wait until audio.IsPlaying is false. After that, load the level like you normally would

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
avatar image
0

Answer by f_Linoal · Oct 31, 2012 at 09:37 PM

The reason why sound stops is that SoundClip and SoundSource are all destroyed while changing level. Use "DontDestroyOnLoad" method and protect GameObjects with sound components.

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

1 Person is following this question.

avatar image

Related Questions

Making sounds wait for each other to finish 2 Answers

Can I call sounds from other script? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Do I have to create an audio source component to play a sound from script? 2 Answers

Getting audio.PlayOneShot to work 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