Hi I’m trying to get audio working on my game but for some reason i keep getting error messages and i have no clue what i am doing ass i am new to all of this.
-Galus07
Error Message: Assets/Player/LaserBlast.cs(18,50): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Here is my code:
using UnityEngine;
using System.Collections;
public class LaserBlast : MonoBehaviour {
public AudioClip LaserSound;
public GameObject LaserPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.Mouse0)) {
AudioClip.Play.LaserSound;
Instantiate(LaserPrefab, transform.position, Quaternion.identity);
}
}
}
I believe you only need to put audio.Play(); with* an audiosource attached.
Ok so I put Audio.Play(LaserSound); instead of AudioClip.Play… but now i get this error message
Assets/Player/LaserBlast.cs(18,31): error CS1502: The best overloaded method match for `UnityEngine.AudioSource.Play(ulong)’ has some invalid arguments
Dman I am Using C# and also i don’t want it for collisions just when the Mouse is clicked
It depends on the sound I guess, if its a SFX use the oneshot. Its audio.Play not Audio.Play,
This is my new code
using UnityEngine;
using System.Collections;
public class LaserBlast : MonoBehaviour {
public AudioClip LaserSound;
public GameObject LaserPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.Mouse0)) {
audio.PlayOneShot(LaserSound);
Instantiate(LaserPrefab, transform.position, Quaternion.identity);
}
}
}
still not working now i get:
MissingComponentException: There is no ‘AudioSource’ attached to the “Player” game object, but a script is trying to access it.
You probably need to add a AudioSource to the game object “Player”. Or your script needs to check if the component is attached before using it.
UnityEngine.AudioSource.PlayOneShot (UnityEngine.AudioClip clip)
LaserBlast.Update () (at Assets/Player/LaserBlast.cs:19)
You have to attach an audiosource to the object like I said in my first post-
Select the object>component>audio>audio source-
havent i done that by saying to play LaserSound?
No, you have to attach an audiosource to any object that has an audio sound.
1 Like
No, you are trying to access an AudioSource which doesn’t exist, hence the error.
You need to add it.
Okay thanks i got it working
ik its 3 years later but… if anyone runs upon this post just use
AudioSource.PlayClipAtPoint (yoursound, transform.position);
this will play the sound wherever the object the script is attached to is.