- Home /
Question by
DarrVell96 · Jan 03, 2014 at 03:35 PM ·
c#audio cliponeshot
Audio OneShot not playing!
HI I have this code
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
public GameObject BulletPreFab;
public AudioClip laser;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp (KeyCode.Mouse0)) //If mouse 0(left click) is released
{
Instantiate(BulletPreFab, transform.position, Quaternion.identity);
audio.PlayOneShot(laser);
//Make an instance of a bullet when mouse 0 is released and keeps the bullet instance position
//related to your spaceship.
}
}
}
For some reason when I press the trigger (mouse 0) the audio clip is not playing?
Any solutions?
Comment
Answer by rocket350 · Jan 05, 2014 at 11:28 AM
You probably need a Audio Source Component attached.
Attach it through the inspector and try running your code again.
Also another way via script is to put this line of code in top of your script
[RequireComponent(typeof(AudioSource))]
and it is better to $$anonymous$$ouse click with