Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

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

Question on Audio? 1 Answer

Help with script for Main Menu? 1 Answer

Ubuntu 16.04 No audio 1 Answer

AudioSource dissappears in variable window on Play 2 Answers

Change Audio Mixer through script for every scene 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