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 Junske · Dec 17, 2014 at 10:13 PM · javascript

wait for audio to be finished than advance

Hi guys, when I raycast a object a audio will be played. right now, it will be played but when I raycast another object, it will play the audio of the other object instead finishing the first audio. I want it to wait for the audio is finished before playing audio 2 (like in waiting for eachother). see my code.

 function Update () {
  if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "kat"))  
  {   
            hitter = true;
            Print("I am looking at" + hit.transform.tag);
            {
            if(audioKathasplayed == false)
            {
            audioKathasplayed = true;
            audio.clip = audioKat;
         audio.Play();
         }
         Timer += Time.deltaTime;
         if (Timer > 20)
         Destroy(hit.collider.gameObject);
         }        
    }
      
    
    else if (Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
    {
            hitter = true;
            Print("I am looking at" + hit.transform.tag);
            {
            if(audioBoekhasplayed == false)
            {
         audioBoekhasplayed = true;
            audio.clip = audioBoek;
         audio.Play();
         }
         Timer += Time.deltaTime;
         if (Timer > 27)
         Destroy(hit.collider.gameObject);
         }        
    }
     
     
  else 
      {    
          hitter = false;
          {
           Timer = 0.0;
           Print("I am looking at nothing");    
           }
       }
        }



  function DestroyObject(){
  if (Physics.Raycast(transform.position, transform.forward, hit))
  {
  if(hit.collider.gameObject.tag == "pen")
      {
      yield WaitForSeconds(audio.clip.length);
      Destroy(hit.collider.gameObject);
      }
  }
  }
Comment
Add comment · Show 3
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 Graham-Dunnett ♦♦ · Dec 17, 2014 at 10:13 PM 0
Share

Not sure you asked a question. What's your problem?

avatar image Junske · Dec 18, 2014 at 12:53 AM 0
Share

i edit it.

avatar image MrSoad · Dec 18, 2014 at 12:55 AM 0
Share

What calls the function "DestroyObject" ?

2 Replies

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

Answer by Junske · Dec 18, 2014 at 01:21 PM

ok I managed to make it work. only thing is when the object get destroy i get "NullReferenceException: Object reference not set to an instance of an object" what am I doing wrong?

EDIT: this is the right way to write it. you guys can use it! thanks to MrSoad

         function Update () {
         
          if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "kat"))  
          {   
                    hitter = true;
                    Print("I am looking at" + hit.transform.tag);
                    if(audio.isPlaying == false)
                    {
                     StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
                 }        
            }
              
            
          else if (Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
            {
                    hitter = true;
                    Print("I am looking at" + hit.transform.tag);
                    if(audio.isPlaying == false)
                    {
                     StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
                 }        
            }
             
            else if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "pen"))
            {
                    hitter = true;
                    Print("I am looking at" + hit.transform.tag);
                    if(audio.isPlaying == false)
                    {
                     StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
                 }    
            }
            
              
          else 
                  {    
                  hitter = false;
                   Timer = 0.0;
                   Print("I am looking at nothing");    
                   }
                }
         
  function PlayAndDestroy(oHit_Object : GameObject){
  
      if(oHit_Object.tag == "kat")
      {
          audio.clip = audioKat;
          audio.Play();
          yield WaitForSeconds(audioKat.length);
          Destroy(oHit_Object); 
      }
      
      else if(oHit_Object.tag == "boek")
      {
          audio.clip = audioBoek;
          audio.Play();
          yield WaitForSeconds(audioBoek.length);
          Destroy(oHit_Object);
      }
      
      else if(oHit_Object.tag == "pen")
      {
          audio.clip = audioPen;
          audio.Play();
          yield WaitForSeconds(audioPen.length);
          Destroy(oHit_Object);
      }
  }
             
     
     
     



Comment
Add comment · Show 5 · 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 MrSoad · Dec 18, 2014 at 01:28 PM 0
Share

PlayAndDestroy() is a coroutine, this does not run in Update, it runs along side it, which is right. The problem is that your raycast is in Update and this may have run again, changing your raycast "hit" data. When you start this coroutine you should pass the current hit data to it so it has a copy of the valid data to use when it runs.

avatar image Junske · Dec 18, 2014 at 01:56 PM 0
Share

could you help me with this? i'm pretty stuck with this one..

avatar image MrSoad · Dec 18, 2014 at 02:10 PM 0
Share

Try this, when you start your coroutines use :

 StartCoroutine(PlayAndDestroy(hit.collider.gameObject));

and alter your PlayAndDestroy function like this :

 function PlayAndDestroy(oHit_Object : GameObject){
 
     if(oHit_Object.tag == "kat")
     {
         audio.clip = audio$$anonymous$$at;
         audio.Play();
         yield WaitForSeconds(audio$$anonymous$$at.length);
         Destroy(oHit_Object); 
     }
     
     else if(oHit_Object.tag == "boek")
     {
         audio.clip = audioBoek;
         audio.Play();
         yield WaitForSeconds(audioBoek.length);
         Destroy(oHit_Object);
     }
     
     else if(oHit_Object.tag == "pen")
     {
         audio.clip = audioPen;
         audio.Play();
         yield WaitForSeconds(audioPen.length);
         Destroy(oHit_Object);
     }
 }
avatar image Junske · Dec 18, 2014 at 03:58 PM 0
Share

its working like a charm!

avatar image MrSoad · Dec 18, 2014 at 03:59 PM 0
Share

Good glad its all working for you :)

avatar image
0

Answer by HYPERSAVV · Dec 18, 2014 at 01:21 AM

You can check to see if a clip is playing using audio.isPlaying(). You are using the same variable (audio) for both Kat and Boek, so this should be doable.

http://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html

However, it still looks like there are issues regarding how you dispose of these objects. Consider something like this:

 if(Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
 {
    // We don't really care if Kat is playing, we care if anything is currently playing
    if(audio.isPlaying() == false)
    {
       StartCoroutine(PlayAndDestroy(audioBoek,hit.collider.gameObject));
    }
 }
     
 IEnumerator PlayAndDestroy(AudioClip clip, GameObject someObject)
 {
    audio = newClip;
    audio.Play();
    yield WaitForSeconds(audio.clip.length);
    Destroy(someObject);
 }



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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved] Generating Particles with Attack Animation 1 Answer

How to create dynamic array of type GameObject using javascript? 2 Answers

accessing a variable from one script in another with Unity 1 Answer

How Do I Create A Nested Function With Unity JavaScript? 1 Answer

How to measure ad impressions in In-Game Ads? 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