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 /
This question was closed Mar 08, 2015 at 11:47 PM by KnightRiderGuy for the following reason:

Too subjective and argumentative

avatar image
0
Question by KnightRiderGuy · Feb 26, 2015 at 04:43 PM · gameobjectaudioaudiosourcescene-switching

Play Sound On Object In Another Scene With Button from First Scene

OK This is going to sound a bit confusing so please bear with my while I try and explain. ;) OK lets say I have a scene we'll call it scene one for simplicity. In this scene I have a UI panel with buttons that I want to play a sound clip with.

Now here comes the possible confusing part.

However I want to play the sounds that my buttons select in scene one on a game object located in scene two. So that when you select a sound in scene one, scene two will load and play that sound on my game object. So far in scene 2 I have this script that when a sound is done playing the scene one will automatically load back up so that another sound can be clicked and then played.

I hope I explained that right.

This is the script I have in scene two, scene one is currently just buttons with sound selections.

 using UnityEngine;
 using System.Collections;
 
 public class WaveformAudioManager : MonoBehaviour {
 
 
     //Exit Waveform Screen after audio has finished playing to Main Logo screen
     void Start()
     {
         audio.Play();
 
         StartCoroutine(LoadAfterDelay1("KnightSpinningLogo_NoKITTintro"));    
     }
 
 
 
     IEnumerator LoadAfterDelay1(string levelName){
         //yield return new WaitForSeconds(2.5f); // wait 1 seconds
         yield return new WaitForSeconds(audio.clip.length); //Waits till Audio is done playing
         Application.LoadLevel(levelName);
         
     }
 }
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

  • Sort: 
avatar image
0

Answer by TRG96 · Feb 26, 2015 at 05:20 PM

you can use this function to keep the script and load it into the next scene

 function Awake () {
          DontDestroyOnLoad (transform.gameObject);
      }

In the script have all the different sounds and check which sound the player clicked on, keep the script and object and take it to the next scene and play the selected sound.

Comment
Add comment · Show 2 · 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 MathieuBarbier · Feb 26, 2015 at 05:27 PM 0
Share

Be careful using DontDestroyOnLoad because if you put it on your script in scene 1 you will have a duplicate every time you are going to come back from scene 2 to scene 1.

I would suggest you to make another scene (let's call it scene 0) with a single gameobject inside that will be used to carry your information from scene 1 to scene 2 and back . You can put a simple script on this gameobject in scene 0 that contains the sound you want to play. Also add this DontDestroyOnLoad line to keep it alive between scene 1 and scene 2.

 DontDestroyOnLoad(this);
 
 AudioClip soundToPlay;
 
 

Then when you select your sound in scene 1 store it in this script. When you load scene 2 just play it from this script.

avatar image KnightRiderGuy · Feb 26, 2015 at 05:41 PM 0
Share

Ok It seems By using the don't destroy on load I can get a sound to play in the next scene only in my game object I have a line renderer that has a Audio Visualizer script attached, now in my 2nd scene the audio play but my line renderer does not react to the sound??? I get this error message:

NullReferenceException: Object reference not set to an instance of an object WaveformAudio$$anonymous$$anager+c__Iterator61.$$anonymous$$oveNext () (at Assets/WaveformAudio$$anonymous$$anager.cs:37) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(IEnumerator) WaveformAudio$$anonymous$$anager:Start() (at Assets/WaveformAudio$$anonymous$$anager.cs:30)

Follow this Question

Answers Answers and Comments

20 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

Related Questions

AudioSource not playig after switching between scenes 0 Answers

How to load audio from server url 1 Answer

Playing a sound clip from another object 1 Answer

master background music toggle not working after changing scenes 1 Answer

Volume Control with Slider 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