Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 A32rock · Jul 09, 2015 at 09:58 PM · timer

Play audio clip after timer

I want to play an audio clip after a certain amount of time, for example, after 1,800 seconds(30 min.) I want to play an audio clip of the player complaining that he is bored. Can someone help me with this please?

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

5 Replies

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

Answer by Sinhabros · Jul 10, 2015 at 08:36 AM

Here is a simple (very simple) timer script that you could use by setting it to 30 seconds. When it is over, you can call AudioSource.Play() method to play the music clip.

 using UnityEngine;
 
 public class Timer
 {
     public float time = 30f; //30 seconds for you
 
     public void Update()
     {
         if (time > 0) {
             time -= Time.deltaTime;
         }
         else {
             Debug.Log("Play Audio Here -- Timer Over!!");
             GetComponent<AudioSource>().Play();
         }
             
 
 }
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
2

Answer by spooneystone · Jul 09, 2015 at 10:29 PM

Use time.time it will give you the time since the game started . Then use something like this

 Float timer;
 
  timer = time.time;
 
 If( timer > 30.00f)
      Play();
 
 

Comment
Add comment · Show 3 · 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 A32rock · Jul 10, 2015 at 01:19 AM 0
Share

Thank you, This will help me a lot!

avatar image A32rock · Jul 10, 2015 at 01:34 AM 0
Share

It is actually saying I need a semicolon on line 5,14 but when I put one there it just gives me more errors

avatar image vividhelix · Jul 10, 2015 at 02:12 AM 0
Share

Post your entire code to figure out the problem.

avatar image
1

Answer by vividhelix · Jul 09, 2015 at 10:53 PM

Here is what I use in my projects:

 public static class MonoBehaviourExtensions
     {
 
         public static void InvokeLater(this MonoBehaviour monoBehaviour, float delay, Action action)
         {
             monoBehaviour.StartCoroutine(_invoke(delay, action));
         }
 
         private static IEnumerator _invoke(float delay, Action action)
         {
             if (delay > 0f)
             {
                 yield return new WaitForSeconds(delay);
             }
 
             action();
 
             yield return null;
         }
     }

You can then use this in any monobehavior like this:

 this.InvokeLater(30, MethodNameHere)

or

 this.InvokeLater(30, ()=>MethodNameHere())

or

 this.InvokeLater(30, ()=>{MethodNameHere();}

where MethodNameHere does whatever you want it to do in 30 seconds.

EDIT: I just wanted to point out this is just a refactoring-friendly version of MonoBehavior.invoke: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html . You can use that but it requires the method name as a string which always irked me.

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 ransomink · Jul 10, 2015 at 03:47 AM

Dunno what language you need or prefer, but I use JavaScript (UnityScript)...so...yeah. The first method uses StartCoroutine: this is a good/great? workaround if you need to use a parameter(s).

 public var m_complainTime      : float;
 public var m_complainClip      : AudioClip;
 
 private var _audio             : AudioSource;
 
 function Start ()
 {
     _audio = GetComponent(AudioSource);
     StartCoroutine(PlayDelayedClip(m_complainTime, m_complainClip));
 }
 
 public function PlayDelayedClip(time : float, clip : AudioClip)
 {
     yield WaitForSeconds(time);
     _audio.PlayOneShot(clip);
 }

However, when using JavaScript it's not necessary to use StartCoroutine, so you can call the function as so:

 function Start ()
 {
     _audio = GetComponent(AudioSource);
     PlayDelayedClip(m_complainTime, m_complainClip);
 }

Or you could do it this way, though using Update() is not preferred since it runs every frame:

 public var m_complainTime      : float;
 public var m_complainClip      : AudioClip;
 
 private var _hasPlayedClip     : boolean;
 private var _timer             : float;
 private var _audio             : AudioSource;
 
 function Start ()
 {
     _audio = GetComponent(AudioSource);
     _hasPlayedClip = false;
     _timer = 0;
 }
 
 function Update ()
 {
     if (!_hasPlayedClip)
     {
         PlayDelayedClip(m_complainTime, m_complainClip);
     }
 }
 
 public function PlayDelayedClip(time : float, clip : AudioClip)
 {
     if (_timer < time)
     {
         _timer += Time.deltaTime;
         Debug.Log(_timer);
     }
     else
     {
         _timer = 0;
         _hasPlayedClip = true;
         _audio.PlayOneShot(m_complainClip);
     }
 }

Hope this helps. This can easily be translated into C# as well...

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 NeverHopeless · Jul 10, 2015 at 04:51 AM

Try like this:

 // Schedule function call after 30 secs
 Invoke("PlayAudioClip", 30.0f);
 
 // Method that plays sound
 void PlayAudioClip()
 {
     myAudioSource.Play();
 }

 // If you want to cancel this function call you can try like:
 CancelInvoke("PlayAudioClip");

Hope it helps!

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

7 People are following this question.

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

Related Questions

timer starts in the menu scene 1 Answer

Convert this string formatting from C# to JS 1 Answer

Start timer with trigger 1 Answer

I want particle to emit for one second after my mouseclick 1 Answer

How do you stop a score counter when you die 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