Hello, I created this script so when you click it shoots the game object, I wanted it also to play a OneShotSound through a AudioClip public variable this is C# btw. But when I do this put the clip in the inspector it says I want an audiosource which I don’t want because I want it to play through the script. Here is the script thanks!
using UnityEngine;
using System.Collections;
public class FireBlaster : MonoBehaviour
{
public GameObject blaster;
private Transform myTransform;
private Transform cameraHeadTransform;
private Vector3 launchPosition = new Vector3();
private float fireRate = 0.2f;
private float nextFire = 0;
public AudioClip gunSound;
void Start ()
{
myTransform = transform;
cameraHeadTransform = myTransform.FindChild ("CameraHead");
}
void Update ()
{
if(Input.GetButton("Fire Weapon") Time.time > nextFire)
{
nextFire = Time.time + fireRate;
launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0.2f);
Instantiate(blaster,launchPosition,Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
myTransform.eulerAngles.y, 0));
audio.PlayOneShot(gunSound);
}
}
}