- Home /
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 typeUnityEngine.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 forPlayOneShot' and no extension method
PlayOneShot' of typeUnityEngine.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 forPlay' 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?
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();
Thank you so much! I hadn't wrapped my head around what each was. I get it now!