• 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 KamLook · Jun 30, 2018 at 06:35 AM · audiosourceplayoneshot

Sound Doesn't trigger when firing projectile

I created a laser sound I want to trigger when a projectile is fired. The projectile launches fine and there is no error when I play the scene but i still cannot hear the sound.


using System.Collections; using UnityEngine;

public class ThrowObject : MonoBehaviour {

 public GameObject projectile;
 public AudioClip shootSound; 

 private float throwSpeed = 2000f;
 private AudioSource source;
 private float volLowRange = 0.5f;
 private float volHighRange = 1.0f;

 // Use this for initialization
 void Awake () {
     source = GetComponent<AudioSource>();
     
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetButtonDown("Fire1"))
     {
         source.PlayOneShot(shootSound, 1F);
         GameObject throwThis = Instantiate (projectile, transform.position, transform.rotation) as GameObject;
         throwThis.GetComponent<Rigidbody>().AddRelativeForce (new Vector3 (0, 0, throwSpeed));
 }

} }


For reference I am following this tutorial (https://www.youtube.com/watch?time_continue=1259&v=5N-428vKwaE) and I am stuck at the 20:48 minute mark. Even after tweaking in the Inspector my sound still does not play.

Comment
Add comment · Show 1
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 CardboardComputers · Jul 01, 2018 at 04:45 AM 0
Share

First off, you don't need both an AudioClip and an AudioSource; your AudioSource already provides the AudioClip as you showed in your editor screenshot. When you want to play the sound, just call source.Play();. Second, and this is kind of silly and I assume your answer to both will be "yes", but

1) are you sure the audio is loud enough to hear? Try playing it in the editor to see if you can hear it, and 2) is the source close enough to the camera, such that falloff does not significantly affect volume?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Adarsh2303 · Jun 30, 2018 at 06:47 AM

@KamLook Just Restart Unity while you Earphone Connected.. I m not sure about this...But I always face this... I don't think your Script has a problem...

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 KamLook · Jun 30, 2018 at 06:52 AM 0
Share

It still does not work after restarting. I can hear footsteps, jumping and landing in my head phones but not this projectile. The code for the footsteps and such was already created in the standard assets i downloaded. Below is a picture of the inspector for the throwing hand component if it helps.

[1]: /storage/temp/119856-pic-to-delete.png

pic-to-delete.png (94.6 kB)
avatar image Adarsh2303 KamLook · Jun 30, 2018 at 06:58 AM 0
Share

Try Changing Sound

if not

Try...source.play(); in place of source.PlayOneShot(shootSound, 1F);

avatar image KamLook Adarsh2303 · Jul 01, 2018 at 02:15 AM 0
Share

Neither of those worked, sorry

avatar image
0

Answer by Grocho · Jun 30, 2018 at 08:41 AM

There are a couple things to look at here, 1st. Do you have multiple audio sources?

If so, you might want to make the Audio Source public an assign it yourself.

2nd. Your source's settings.

Post a screenshot of them so I can check it.

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 KamLook · Jul 01, 2018 at 01:51 AM 0
Share

alt text

I also made the audio source public and assigned it to ThrowingHand but that did not work either.

pic-of-settings.png (80.2 kB)
avatar image KamLook · Jul 01, 2018 at 02:56 AM 0
Share

Actually I do have two Audio sources, one for my player walking and moving and one for firing the gun. How would that interfere with my script above?

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

91 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 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

AudioSource plays noisy audio 1 Answer

AudioSource.PlayOneShot only works ONCE on Unity 5 - FIX 1 Answer

Using PlayOneShot for multiple clip is OK? 0 Answers

My AudioClip wont loop even though loop is true 1 Answer

Need help with AudioSources 1 Answer


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