• 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 carnivoris · Jun 16, 2014 at 01:21 AM · audioaudiosourceaudioclipplayoneshot

Can't get PlayOneShot() or Play() to work

I'm trying to get gun sounds to work, but I'm having trouble getting PlayOneShot() or Play() to work. Here is my code:

     using UnityEngine;
     using System.Collections;
     
     public class Guns : MonoBehaviour {
         public Rigidbody projectile;
         public float speed = 6000;
         public AudioSource mg_spinup;
         public AudioSource minigun_firing;
         public AudioSource mg_spindown;
         
         // Update is called once per frame
         void Update () {
     
             if (Input.GetButton ("Fire1")) {
     // This line fires the projectile, timing of this is off, don't worry about it for now.
                 Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
                 instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
    // This gun spins up before it starts firing, so here's the spinup sound
                 mg_spinup.PlayOneShot(mg_spinup, 1.0f);
     
     
                 if (Input.GetButton ("Fire1")){
             // While the fire button is being pressed, play the minigun firing sound
                     minigun_firing.Play(minigun_firing,4.0f);
                 }
             }
             
             if (Input.GetButtonUp ("Fire1")) {
              // When the player releases the fire button, spin down the weapon.
                     mg_spindown.PlayOneShot(minigun_spindown, 1.0f);
             } 
         }
     }
 

Unity currently throws the following error in the console:

Assets/My Assets/Scripts/CSharp/Guns.cs(19,35): error CS1503: Argument #1' cannot convert UnityEngine.AudioSource' expression to type UnityEngine.AudioClip' If I switch my AudioSources to AudioClips, I get this error: > Assets/My Assets/Scripts/CSharp/Guns.cs(19,35): error CS1061: Type UnityEngine.AudioClip' does not contain a definition for PlayOneShot' and no extension method PlayOneShot' of type UnityEngine.AudioClip' could be found (are you missing a using directive or an assembly reference?) > Assets/My Assets/Scripts/CSharp/Guns.cs(23,48): error CS1061: Type UnityEngine.AudioClip' does not contain a definition for Play' and no extension method Play' of type `UnityEngine.AudioClip' could be found (are you missing a using directive or an assembly reference?)

So, it seems as though Unity doesn't want to use either AudioSources or AudioClips... what am I doing wrong here?

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
4
Best Answer

Answer by AlucardJay · Jun 16, 2014 at 02:26 AM

You are mixing up what an audio clip and an AudioSource is.

To Play Audio : http://docs.unity3d.com/ScriptReference/AudioSource.Play.html

Assign a clip to the AudioSource, tell the AudioSource to Play :

 public AudioClip mySound;
 public AudioSource mySource;

 // in method, assign the clip to the audioSource
 mySource.clip = mySound;
 // AudioSource.Play();
 mySource.Play();

To PlayOneShot : http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html

assign the clip to play and the volume in one command :

 public AudioClip mySound;
 public AudioSource mySource;
 public float myVolume = 1.0f;

 // in method
 // AudioSource.PlayOneShot( clip, volume );
 mySource.PlayOneShot( mySound, myVolume );


It is hard to help any further without knowing if you are trying to assign clips to your variables or AudioSources.

If it is an AudioSource, your PlayOneShot would look like this :

 // the clip is already assigned on the AudioSource
 mg_spinup.PlayOneShot( mg_spinup.clip, 1.0f );

and your play would look like this :

 // the clip is already assigned on the AudioSource
 minigun_firing.volume = 4.0f;
 minigun_firing.Play();





Comment
Add comment · Show 1 · 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 carnivoris · Jun 16, 2014 at 02:55 AM 0
Share

Thank you so much! I hadn't wrapped my head around what each was. I get it now!

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

22 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

Related Questions

Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers

Sound wont play when triggered (javascript) 1 Answer

Why PlayOneShot is not working? C# 1 Answer

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 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