Hello everyone,
am trying to play a simple audio in my game, i have a cube object with only this script :
using UnityEngine;
using System.Collections;
public class testSound : MonoBehaviour
{
public AudioClip loose ;
// Use this for initialization
void Start ()
{
audio.PlayOneShot(loose);
}
// Update is called once per frame
void Update ()
{
}
}
and in the inspector i added the sound file to the component, how ever each time i got this error :
There is no ‘AudioSource’ attached to
the “Cube” game object, but a script
is trying to access it. You probably
need to add a AudioSource to the game
object “Cube”. Or your script needs to
check if the component is attached
before using it.
thank you
1# solution:
You have to attach AudioSource to “Cube” gameObject.
Select your gameObject and click Component >> Audio >> AudioSource.
2# solution
Change
audio.PlayOneShot(loose);
to
AudioSource.PlayClipAtPoint(loose, this.transform.position);
You probably just hooked an audio file to the loose variabe of your script. You actually need to add an AudioSource component to the GameObject