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 ske66 · Dec 30, 2014 at 10:35 PM · javascriptmenuscenesmusicmain

How do I make music continue through certain scenes

I am still fairly new to Unity and Javascript Development and I am working on a 2d platformer with a fully functioning main menu. At this moment in time, the main menu consists of multiple scenes including settings, credits and a start game button that takes you to a scene called testlevel.

I want my main menu to have music separate to that of my credits screen and that of the game scene however I am struggling to code it to work. In the MainMenu scene there is a camera that hosts a script named MusicGenerator that reads as follows -

pragma strict

ar music : GameObject;

function Start () {

 var G : GameObject = GameObject.FindGameObjectWithTag("GameController");
 if(!G) {
     Instantiate(music, Vector3.zero, Quaternion.identity);
 }
 

}

there is prefab connected to this script named music. This prefab has another script attached that reads as follows

pragma strict

function Start () {

 DontDestroyOnLoad(gameObject);
 

}

Of course there is a music source attached to this prefab with an audio file playing at all times.

When these scripts work together they basically search for a prefab named music in the current scene. If no such prefab can be found it creates a new instance. If it finds one that is already there, it will create a new instance of the prefab and continue the music. However what I want is for the script to be able to be altered so that different music can be played and continued throughout different scenes

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 AlucardJay · Dec 31, 2014 at 12:17 AM 0
Share

http://answers.unity3d.com/questions/413203/continuous-music-across-multiple-scenes.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Josh1231 · Dec 31, 2014 at 12:10 AM

you'd have to have a new variable with an array of all of the sound clips:

 public AudioClip[] songs;

you would then change the audio clip:

 AudioSource as = G.GetComponent();
 as.clip = songs[Random.Range(0,songs.Length)];
 as.Play();

you can also wait until the song finishes:

 AudioSource as = G.GetComponent();
 yield WaitForSeconds(as.clip.Length - as.time); //This is the only JS line here, the rest is C#
 as.clip = songs[Random.Range(0,songs.Length)];
 as.Play();

or, alternatively:

 public GameObject[] songs;

then you would delete the original object:

 AudioSource as = G.GetComponent();
 Destroy(G) //you can wait until song end with Destroy(G,as.clip.Length - as.time);
 yield WaitForSeconds(as.clip.Length - as.time);
 Instantiate(songs[Random.Range(0,songs.Length)],Vector3.zero,Quaternion.identity);

I'd recommend the one at the top, because you don't lose the original object, and you wouldn't need something like garbage collecting because you instantiated too much objects.

Comment
Add comment · Show 4 · 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 ske66 · Jan 02, 2015 at 04:19 PM 0
Share

Do I add this script to the main camera?

avatar image Josh1231 · Jan 03, 2015 at 09:12 AM 0
Share

yes, and you want to make sure that the object with the song has don't destroy on load

avatar image ske66 · Jan 05, 2015 at 10:05 AM 0
Share

Brilliant, thanks!

avatar image vexe · Jan 09, 2015 at 10:44 PM 1
Share

@ske66 if the answer helped, make sure you +1 and mark as correct (a way of saying thanks and showing appreciation)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to set a texture and another font in a GUI Text 2 Answers

How I can prevent music from stopping after changing a scene? 3 Answers

How to stop menu music when loading level (1,2,3,..,x) 1 Answer

mouse over for 3d menu 1 Answer

Continuous AudioSource in DontDestroyOnLoad 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