Question by
Cluelessguy · Apr 10, 2016 at 07:05 PM ·
erroraudiodelayunexpected-symbol
Error CS1519 help
I'm trying to make a script where a gunshot goes off after a certain amount of time.
using UnityEngine;
using System.Collections;
public class Gunshot : MonoBehaviour {
public AudioClip Gun;
private float volLowRange = .5f;
private float volHighRange = 1.0f;
private AudioSource source
public float timeLeft = 5.0f;
void Awake () {
source = GetComponent<AudioSource>();
}
public void Update()
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0.0f)
{
float vol = Random.Range (volLowRange, volHighRange);
source.PlayOneShot(Gun,vol);
}
}
Comment
Best Answer
Answer by Oribow · Apr 10, 2016 at 07:10 PM
You are missing a final } in your script and a ; after this private AudioSource source
Fixed it here for you:
using UnityEngine;
using System.Collections;
public class Gunshot : MonoBehaviour
{
public AudioClip Gun;
private float volLowRange = .5f;
private float volHighRange = 1.0f;
private AudioSource source;
public float timeLeft = 5.0f;
void Awake()
{
source = GetComponent<AudioSource>();
}
public void Update()
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0.0f)
{
float vol = Random.Range(volLowRange, volHighRange);
source.PlayOneShot(Gun, vol);
}
}
}
Thank you. But now it plays as soon as the scene begins anyways I can fix that?
Look in the Inspector. On your AudioSource is a property called PlayOnWake. Just turn it off. Dont forget to accept the answer :)
That isn't working I probably have to change the time interval?
Do you have still the problem? I cant see a reason for it in the code. Do you actually want it to auto-shoot like it does know?
Yes I am. Yeah I just the audio clip to play after 3 seconds.