- Home /
There is no 'AudioSource' attached to the
I'm pretty new to unity as well as game programing and other stuffs. I am getting this error " There is no 'AudioSource' attached to the "Character" game object, but a script is trying to access it." Here goes my code.
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.tag == "bomb")
{
// I got hit by a bomb!
Instantiate(explosion, col.gameObject.transform.position, Quaternion.identity);
audio.PlayOneShot(explosionSFX);
}
else if (col.gameObject.tag == "stein")
{
animation.Play("catch");
audio.PlayOneShot(catchSFX);
}
col.gameObject.transform.position.y = 50;
col.gameObject.transform.position.z = -16;
col.gameObject.transform.position.x = Random.Range(0,60);
}
Dragged and dropped the audio source from my folder in project panel, (tried it in prefab, Player game object in hierarchy and in code). nothing gives me desired the output. Can any one please help me?
So, you have an AudioSource component on the same gameObject as this script?
Not actually a component. I've declared them in the beginning of the script and they were available for me in the inspector panel.
Answer by syclamoth · Jan 23, 2012 at 12:37 PM
You need to add an AudioSource component to the object. Keep in mind that if you declare
var audio : AudioSource;
it will hide the Component.audio lookup variable, which finds and returns any AudioSource component that is attached to the same GameObject.
Answer by ArunChnadran · Jan 24, 2012 at 05:39 AM
But I got this working without adding any audio component.
var speed:int;
var audioClips:AudioClip[];
//some functions
function Explode(pos:Vector3)
{
audio.PlayOneShot(audioClips[Random.Range(0, audioClips.length)]);
Instantiate(explosion, pos, Quaternion.identity);
}
for this, I just dragged and dropped the files in to the gameObject and pressed apply(for its a prefab) and I could here different sounds.
Well, that's odd. 'audio' should return null if there is no AudioSource attached to the same object.
Answer by karl_ · Jan 24, 2012 at 05:56 AM
Add this line to your script above the Class declaration:
[RequireComponent(typeof(AudioSource))]
or in JS
@script RequireComponent(AudioSource)
This automatically adds the Component you specify. Alternatively, you could just add the component manually. (Component/Audio/Audio Source). From here, calling audio.Whatever() will automatically get the AudioSource component for you. Now you can load up clips to the AudioSource like this:
@script RequireComponent(AudioSource)
var myClip : AudioClip;
function Start() { audio.clip = myClip; // Sets the audio source's current clip to 'myClip' audio.Play(); // Actually plays the clip.
}
mate this method gave me the same error! But I whole heartedly appreciate your help and expect them in future! thanx!
Answer by venkspower · Jan 24, 2012 at 07:11 AM
Otherwise, make a GameObject. Add an AudioSource, and import the required audio file to this GameObject (which should be of .wav or .mp3 format). And attach this GameObject to the script. And activate this GameObject, whenever required. This should work fine.