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 /
This question was closed Apr 15, 2016 at 09:00 PM by CodewithFin for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by CodewithFin · Apr 14, 2016 at 06:21 PM · c#music

Music code not playing music.

I'm trying to play music fluently between my 'MainMenu' and 'Settings' scenes but the code I have wont play the music, it doesn't show any errors and I can't find anything wrong with the code.

Code:

 //Keeps the music playing fludently throughout 'MainMenu' and 'Settings'
 static bool AudioBegin = true;
 void Awake()
 {
     if (!AudioBegin)
     {
         Debug.Log("Music");
         GetComponent<AudioSource>().Play();
         DontDestroyOnLoad(gameObject);
         AudioBegin = true;
     }
 }
 void Update()
 {
     if (Application.loadedLevelName == "MainScene")
     {
         Debug.Log("Music2");
         GetComponent<AudioSource>().Stop();
         AudioBegin = false;
     }
 }
Comment
Add comment · Show 2
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 Blue-Cut · Apr 14, 2016 at 06:53 PM 0
Share

So the purpose of your $$anonymous$$ainScene is to stop the music ? And your SettingScene plays the music only if it's loaded after the $$anonymous$$ainScene ? That's what I'm reading.

"I'm trying to play music fluently between my '$$anonymous$$ain$$anonymous$$enu' and 'Settings'" So you play a $$anonymous$$usic in your scene, and when you go to the scene settings, your music keeps playing normally. That is what I understand. Can you tell me if I am write or wrong in my understanding. Because I made a project that has this feature.

avatar image CodewithFin Blue-Cut · Apr 14, 2016 at 07:07 PM 0
Share

@Blue Cut Yeah, so I want the music to play fluently between the $$anonymous$$ain $$anonymous$$enu and Settings but when I start the game I want it to start playing different music. I also have a volume slider that I want to control the volume of the music throughout the whole game but I have the code for the volume slider already I just need help on the fluent music part. But yes, you're correct.

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Murkas · Apr 14, 2016 at 07:13 PM

The only point where music could be played is within the Awake-function. This function is called only once. Since your AudioBegin-Boolean is set to "true" at the beginning it will never play.

You should take a look at Unity's Event execution order. I think using "OnLevelWasLoaded" could help. Using "Update" is not that good, since it is called ever frame. You don't want to stop your music every frame.

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 CodewithFin · Apr 14, 2016 at 07:20 PM 0
Share

@$$anonymous$$urkas I'm attempting to play music on my $$anonymous$$ain $$anonymous$$enu and Settings Scenes, not my '$$anonymous$$ainScene'(Sorry, I wasn't clear about that) so it would be correct to stop the music on my '$$anonymous$$ainScene'.

avatar image
0

Answer by churu · Apr 14, 2016 at 07:59 PM

Well.... Someone would be Try just creating a audio source

Public audioclio namehere;

audiosource secondname;

void Start() { secondname= GetComponent(); secondname.clip = namehere;

}

void yourstuffhere() { namehere.play()

}

Then use the loop function unity offers https://i.gyazo.com/903b5cb184d04c32e70de5fe6f4e3fdb.png

to stop playing the music simply go with namehere.pause()

Im just a beginner myself but this could be something to experiment with.

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 Blue-Cut · Apr 14, 2016 at 08:03 PM

You should take a look at this : https://docs.unity3d.com/ScriptReference/AudioSource-time.html

So you have 3 scenes : - MainMenu => music1 - Settings => music1 - MainScene => music2

What you need to do basically is :

  void Start()
  {
          GetComponent<AudioSource>().Play();
  }

But you should not try to keep your gameobject alive between the scenes with music1 and the scene with music2. First of all, just do a script that plays the music at Start, and set the correct audioSource in each scene.

Having music1 playing fluently between your 2 scenes is a next step.

I think that in my project, we have dealed with the time. If you quit your scene at time=4 you begin the music at time=4 in the next scene.

I am not sure about my project, if your problem is not too urgent or if you can't find a solution, I can check that tomorrow. But dealing with objects that stay alive from one scene to another can be a bit tricky I guess, that is not something I recommand if you can avoid doing this.

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

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Hey I have problem with my music game script audio source,Hello I have problem with my MusicAudioSource script 0 Answers

Need Help With Music 0 Answers

C# DontDestroyonLoad How? 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