• 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
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

People who like this

0 Show 0
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

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

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

Comment

People who like this

0 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Adding Audio clip in scripting 1 Answer

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

C sharp cant play Audio 2 Answers

Too many AudioSources? 1 Answer

Timeline AudioClip ease not working 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