• 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 /
  • Help Room /
avatar image
0
Question by Cluelessguy · Apr 13, 2016 at 10:12 PM · scripting problemaudiodelaynotworking

Script plays sound at incorrect time

I wrote a script that plays a sound after 3 seconds and set it to play on awake which made it play right away. So I tried unchecking Play on awake it doesn't play at all. Where is the error?

 using UnityEngine;
 using System.Collections;
 
 public class Gunshot : MonoBehaviour
 {
     public AudioClip Gun;
     
     private float volLowRange = .5f;
     private float volHighRange = 1.0f;
     private AudioSource source;
     public float timeLeft = 5.0f;
     
     void Awake()
     {
         
         source = GetComponent<AudioSource>();
     }
     
     
     
     public void Update()
     {
         timeLeft -= Time.deltaTime;
         
         if (timeLeft <= 0.0f)
         {
             float vol = Random.Range(volLowRange, volHighRange);
             source.PlayOneShot(Gun, vol);
         }
     }
 }
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
0

Answer by TBruce · Apr 13, 2016 at 11:09 PM

@Cluelessguy This should fix your problem. I would just like to note that timeLeft is set to 5 seconds and not 3.

using UnityEngine; using System.Collections;

public class Gunshot : MonoBehaviour { public AudioClip Gun;

 private float volLowRange = .5f;
 private float volHighRange = 1.0f;
 private AudioSource source;
 public float timeLeft = 5.0f;

 void Awake()
 {
     source = GetComponent<AudioSource>();
     if (audioSource == null)
     { // if AudioSource is missing
         Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
         // let's just add the AudioSource component dynamically
         source = gameObject.AddComponent<AudioSource>(timeLeft);
     }
     if (Gun != null)
     {
         StartCoroutine(PlaySoundAfterDelay());
     }
 }

 IEnumerator PlaySoundAfterDelay(float delay)
 {
     yield return new WaitForSeconds(delay);
     source.PlayOneShot(Gun, vol);
 }

}

Comment
Add comment · Show 2 · 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 Cluelessguy · Apr 14, 2016 at 02:08 AM 0
Share

I'm getting compiler errors.

avatar image TBruce Cluelessguy · Apr 14, 2016 at 03:07 AM 0
Share

@Cluelessguy $$anonymous$$y apologies. This is what your script should have been

 using UnityEngine;
 using System.Collections;
 
 public class Gunshot : $$anonymous$$onoBehaviour
 {
     public AudioClip Gun;
 
     private float volLowRange = .5f;
     private float volHighRange = 1.0f;
     private AudioSource source;
     public float timeLeft = 5.0f;
 
     void Awake()
     {
         source = GetComponent<AudioSource>();
         if (source == null)
         { // if AudioSource is missing
             Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
             // let's just add the AudioSource component dynamically
             source = gameObject.AddComponent<AudioSource>();
         }
 
         if (Gun != null)
         {
             StartCoroutine(PlaySoundAfterDelay(timeLeft));
         }
     }
 
     IEnumerator PlaySoundAfterDelay(float delay)
     {
         yield return new WaitForSeconds(delay);
         float vol = Random.Range(volLowRange, volHighRange);
         source.PlayOneShot(Gun, vol);
     }
 
 }
 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

60 People are following this question.

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

Related Questions

Help with script for Main Menu? 1 Answer

Ubuntu 16.04 No audio 1 Answer

AudioSource dissappears in variable window on Play 2 Answers

OnTriggerEnter Footsteps Multiple Times A Second 1 Answer

Question on Audio? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges