Play Sound at Certain Time

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.

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.

Do you know the script for that. I am new to scripting.

What language are you using in Unity?

add

public float starttime;

assign that when you want the timer to start

void Start(){
starttime = Time.time;
}

and then in update

void Update(){
        if (Time.time - starttime == 25) {
            Debug.Log ("Waited");
            starttime = Time.time; // setting start time here will loop the timer & wait every 25 seconds
        }
        }

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:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(AudioSource))]
public class PlaySound : MonoBehaviour
{
    private float playEverySeconds = 25;
    private float timePassed = 0;
    private AudioSource myAudioSource;

    void Start()
    {
        myAudioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        timePassed += Time.deltaTime;
        if (timePassed >= playEverySeconds)
        {
            timePassed = 0;
            myAudioSource.Play();
        }
    }
}

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! :slight_smile:

4 Likes
void Start(){
     Invoke("PlaySound", 25.0f);
}

void PlaySound(){
  //play
 
}

This script looks nice and simple, so how do I put the sound I want where it says //play?

So where do I put the sound I want in the script?

Java and C#

The sound goes in the inspector of the gameobject holding the script. To assign it, you must first add the sound to your project.

Oh I see, I thought you had to put it in the script somewhere, but that is a lot easier. Thanks! :smile:

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:

public AudioClip mySound; // add sound via inspector

    void Start()
    {
        Invoke("PlaySound", 25.0f);
    }

    void PlaySound()
    {     
        //play
        if (!GetComponent<AudioSource>()) // this object needs an Audio Source to play sounds
        {
            gameObject.AddComponent<AudioSource>(); // don't have one?   Add one
        }

        if (mySound) // only play the sound if one exists or there be errors
        {
            audio.PlayOneShot(mySound);
        }
    }

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.

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

Use invoke with time like someone else suggested, or use coroutine (IEnumerator functions) to have things timed in the function

Ok, thanks for the tip. :smile: