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 GregsGotGame · Mar 24, 2015 at 06:50 AM · c#audiodisable

Disabling a script inside a coroutine(look at my comment below)?

Hi I wrote a script to play a sound after the if statement activates, but nothing happens. I've tried a lot of different things. Here is the script I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class TimerScript : MonoBehaviour 
 {
     public GUIText clockTime;
     public GUIText loseMessage;
 
     public GameObject rift;
     public GameObject tryAgainButton;
     public GameObject returnButton;
 
     public Rigidbody kineticBody;
 
     private float timeLeft;
 
     public AudioClip[] loseSound;
 
     void Start ()
     {
         kineticBody = GetComponent<Rigidbody> ();
         timeLeft = 30;
         clockTime.text = "Time: 00:" + timeLeft.ToString();
         loseMessage.text = "";
     }
 
     void Update ()
     {
         timeLeft -= Time.smoothDeltaTime;
         clockTime.text = "Time: 00:" + timeLeft.ToString ();
         if (timeLeft <= 0) 
         {
             loseMessage.text = "You Failed!";
             clockTime.text = "Time: 00:00";
             tryAgainButton.SetActive(true);
             returnButton.SetActive(true);
             kineticBody.isKinematic = true;
             rift.SetActive(false);
             PlaySound(0);
 
         }
     }
 
     void PlaySound (int noise)
     {
         audio.clip = loseSound[noise];
         audio.Play ();
     }
 }

I'm new to Unity so I'm not sure what I'm doing wrong.

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

· Add your reply
  • Sort: 
avatar image
1

Answer by supernat · Mar 24, 2015 at 06:56 AM

It's probably 1 of 2 things or both. 1) make sure you assigned a sound clip to the loseSound array in the Inspector. 2) Your Update() method is going to run every frame, so once the timer runs out, you're going to call PlaySound every frame which will restart your clip to the first sample every frame. You can fix this by wrapping it with a "if (!audio.isPlaying)" check before calling PlaySound, or you could add a new flag "gameOver" which you set to true at line 40 and test that it is false at line 31.

Comment
Add comment · Show 8 · 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 GregsGotGame · Mar 24, 2015 at 09:46 PM 0
Share

Thanks supernat! It works great now.

avatar image GregsGotGame · Mar 29, 2015 at 10:31 PM 0
Share

I've got another problem. After PlaySound(); or line 39, I want to disable the whole script after a few seconds. The reason I want to disable the script is ,because I no longer need it after PlaySound();.

P.S. I've changed the void PlaySound() into an IEnumerator.

P.S.S. Here is the updated script if you want to look at it:

 using UnityEngine;
 using System.Collections;
 
 public class TimerScript : $$anonymous$$onoBehaviour 
 {
     public GUIText clockTime;
     public GUIText lose$$anonymous$$essage;
     
     public GameObject tryAgainButton;
     public GameObject returnButton;
 
     private float timeLeft;
 
     public AudioClip loseSound;
 
     void Start ()
     {
         timeLeft = 30;
         clockTime.text = string.Format("{0:0.0}", timeLeft);
         lose$$anonymous$$essage.text = "";
     }
 
     void Update ()
     {
         timeLeft -= Time.deltaTime;
         clockTime.text = string.Format("{0:0.0}", timeLeft);
         if (timeLeft <= 0) 
         {
             Time.timeScale = 0f;
             lose$$anonymous$$essage.text = "You Failed!";
             tryAgainButton.SetActive(true);
             returnButton.SetActive(true);
             if(!audio.isPlaying)
             {
                 StartCoroutine(PlaySound());
             }
         }
     }
 
     IEnumerator PlaySound ()
     {
         audio.clip = loseSound;
         audio.pitch = Random.Range (0.5f, 0.6f);
         audio.Play ();
         yield return new WaitForSeconds (audio.clip.length);
         //I want to disable the script here
     }
 }

avatar image adelphiaUK · Mar 29, 2015 at 11:17 PM 0
Share

Are you saying you never need the script again after the sound has finished or just want the sound to stop playing?

If you just want the sound to stop then just remove looping in the inspector or you could add audio.Stop() immediately after the yield.

Also, I'm not sure if that will work as you are setting timeScale to 0 and then trying to perform actions after it's set to zero. I could be wrong there though, as I tend to use a gamePaused flag (bool) rather than zero time scale.

Which version of Unity are you using by the way? I would strongly recommend switching to 4.6 or 5 and use the new UI system if you haven't already.

avatar image GregsGotGame · Mar 29, 2015 at 11:30 PM 0
Share

Yeah I don't need the script after the sound. $$anonymous$$y audio isn't on loop, but it still plays strangely enough. Also, if timeScale = 0 isn't what I'm looking for, how do I implement the gamePaused flag. Finally, the version of Unity I'm using is 4.6, but I might upgrade to 5 in the future.

avatar image flashframe · Mar 30, 2015 at 02:59 PM 1
Share

Yes, you need to put your timer in a while loop with a yield, so that the timer counts down before the rest of your code is activated.

   while(timeLeft > 0){
     timeLeft -= Time.deltaTime;
     yield return null;
     }

//Once the timer has finished, do everything else

Show more comments

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

using Aim Constraint 1 Answer

Passing Java String to C# for use in Wwise Post Event = Fail 0 Answers

Add audio. 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