Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Play Sound at Certain Time

Discussion in 'Scripting' started by thechubbymonkey, Jan 8, 2015.

  1. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    I am making a game when 25 seconds have passed it plays a sound. Anyone know the script for that. Do I also have to do anything else with components and other stuff like that? Please tell me the script, that would be great. Thanks.
     
  2. jorgemk85

    jorgemk85

    Joined:
    Jun 24, 2013
    Posts:
    16
    You could just declare a float as a class member and add to it the Time.deltaTime value. Then check when it reaches 25 (or more) and trigger the sound and reset the float to 0.

    Do this in the Update method.
     
  3. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Do you know the script for that. I am new to scripting.
     
  4. jorgemk85

    jorgemk85

    Joined:
    Jun 24, 2013
    Posts:
    16
    What language are you using in Unity?
     
  5. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    add
    Code (CSharp):
    1. public float starttime;
    assign that when you want the timer to start
    Code (CSharp):
    1. void Start(){
    2. starttime = Time.time;
    3. }
    and then in update
    Code (CSharp):
    1. void Update(){
    2.         if (Time.time - starttime == 25) {
    3.             Debug.Log ("Waited");
    4.             starttime = Time.time; // setting start time here will loop the timer & wait every 25 seconds
    5.         }
    6.         }
     
  6. jorgemk85

    jorgemk85

    Joined:
    Jun 24, 2013
    Posts:
    16
    Create a new C# script inside your project and name it "PlaySound" (without quotes), then open it and replace everything inside it with the following:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(AudioSource))]
    5. public class PlaySound : MonoBehaviour
    6. {
    7.     private float playEverySeconds = 25;
    8.     private float timePassed = 0;
    9.     private AudioSource myAudioSource;
    10.  
    11.     void Start()
    12.     {
    13.         myAudioSource = GetComponent<AudioSource>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         timePassed += Time.deltaTime;
    19.         if (timePassed >= playEverySeconds)
    20.         {
    21.             timePassed = 0;
    22.             myAudioSource.Play();
    23.         }
    24.     }
    25. }
    Finally, attach the script to a GameObject in your Scene. Select the same gameObject and you will notice in the Inspector an AudioSource Component will be attached automatically to it and inside it, a slot to select the AudioClip it will use. Make sure to remove the check in the "Play on awake" option.

    And thats it! :)
     
  7. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Code (CSharp):
    1. void Start(){
    2.      Invoke("PlaySound", 25.0f);
    3. }
    4.  
    5. void PlaySound(){
    6.   //play
    7.  
    8. }
     
  8. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    This script looks nice and simple, so how do I put the sound I want where it says //play?
     
  9. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    So where do I put the sound I want in the script?
     
  10. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Java and C#
     
  11. jorgemk85

    jorgemk85

    Joined:
    Jun 24, 2013
    Posts:
    16
    The sound goes in the inspector of the gameobject holding the script. To assign it, you must first add the sound to your project.
     
  12. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Oh I see, I thought you had to put it in the script somewhere, but that is a lot easier. Thanks! :D
     
  13. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373

    Sorry, I thought you had the sound playing part down already.

    http://docs.unity3d.com/ScriptReference/AudioClip.html <- an audio file you want to play
    http://docs.unity3d.com/ScriptReference/AudioSource.html <- component to play audio
    http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html <- I'll use this method since its easiest to set up

    Basically something like this then:

    Code (CSharp):
    1. public AudioClip mySound; // add sound via inspector
    2.  
    3.     void Start()
    4.     {
    5.         Invoke("PlaySound", 25.0f);
    6.     }
    7.  
    8.     void PlaySound()
    9.     {    
    10.         //play
    11.         if (!GetComponent<AudioSource>()) // this object needs an Audio Source to play sounds
    12.         {
    13.             gameObject.AddComponent<AudioSource>(); // don't have one?   Add one
    14.         }
    15.  
    16.         if (mySound) // only play the sound if one exists or there be errors
    17.         {
    18.             audio.PlayOneShot(mySound);
    19.         }
    20.     }
    EDIT: Some things to note: 3d sounds are affected by where they are positioned and if your audio listener isn't in range... the sound isn't heard. For a one time effect or something you may want your sound to not be a 3d sound so it can be heard no matter what.
     
    Last edited: Jan 9, 2015
  14. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63

    Thanks for the script and the help. I will try to edit my game with sounds right away.:)
     
  15. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    Use invoke with time like someone else suggested, or use coroutine (IEnumerator functions) to have things timed in the function
     
  16. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Ok, thanks for the tip. :D