Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tabbott87 · Sep 23, 2011 at 04:31 PM · audioaudiosourceaudioclipmusic

Unity Unable to Reassign Audio Clip

Hey All,

I am using one audio source throughout all my game levels that handles playing music for my game, which uses DontDestroyOnLoad();. The code I have posted below attempts to switch the audio clip when it is needed from one track to another, but for some reason it just isn't working. I've been looking at this for hours trying to figure out some hack, but I can't figure it out.

 var menuAudio : AudioClip;
 var battleAudio : AudioClip;

 // Set the lastLevel variable to a value that will force the menu music to start
 // playing at the beginning of the game.
 private var lastLevel : int = 10;

 // Called every time a new level loads by a game object that exists individually
 // in each scene.
 function CheckLevel () 
 {
 // Should the menu track be playing right now?
 if (Application.loadedLevel < 2)
 {
     Debug.Log("Is Menu Level");
     
     // Are we coming from a battle level? If so, start playing the menu track.
     // This makes sure that the track doesn't restart and continues playing
     //         through menu scenes.
     
     if (lastLevel >= 2)
     {
         gameObject.audio.clip = menuAudio;
         gameObject.audio.Play();
         Debug.Log("Play Menu");
     }
 }
 // Should the battle track be playing right now?
 else if(Application.loadedLevel >= 2)
 {
     Debug.Log("Is Battle Level");
     
     // Are we coming from a menu level? If so, start playing the battle track.
     // This makes sure that the track doesn't restart and continues playing
     //         through battle scenes.
     if (lastLevel < 2)
     {
         gameObject.audio.clip = battleAudio;
         gameObject.audio.Play();
         Debug.Log("Play Battle");
     }
 }
 
 // Set the last level equal to the current level, so it can be referenced next
 // time the CheckLevel function is called.
 lastLevel = Application.loadedLevel;

}

The weird thing is, that the Audio Source is not set to Play On Awake. So the script works the first time, and it successfully changes the music when needed to the battle track, but when it needs to switch from the battle track to the menu music, it just continues to play the battle music. It even gets to the Debug.Log("Play Menu"); line, but Unity is just blatantly ignoring the lines that reassign audio. I can put any code into that if statement, and unless it is a Debug.Log, it is ignored.

Any suggestions?

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
0

Answer by Design3.com · Sep 23, 2011 at 09:10 PM

Have you tried calling audio.Stop() before switching out the clips?

Comment
Add comment · Show 1 · 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 tabbott87 · Sep 23, 2011 at 10:14 PM 0
Share

Yeah, anything that I do to modify the AudioSource gets completely ignored by Unity. I added a line: Debug.Log(audio.clip); and it is registering the fact that the clip is different, but the GUI doesn't show the update and the music ignores it. Which makes me think it might be a Unity bug...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to stop music from restarting when reloading scene it began? 0 Answers

C sharp cant play Audio 2 Answers

Adding Audio clip in scripting 1 Answer

Strange AudioSource issue, hogging resources 1 Answer

Event at audio position? 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