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 methodPlayOneShot' 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: TypeUnityEngine.AudioClip' does not contain a definition for
Play’ and no extension methodPlay' 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?