- Home /
I get the error "MissingComponentException: There is no 'AudioSource' attached .."
I am new to Unity. I am going through 'Unity Game Development Essentials'. I have written code in VB.NET and so I think I would be more comfortable writing scripts in C#. I try converting one of the Javascripts in the book to C#. When I run the script in game mode I get the error "MissingComponentException: There is no 'AudioSource' attached ..". I did post this issue on another thread but as an unregister user. So I thought I would post as a register user. I listed my code below. What am I doing wrong?
using UnityEngine; using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class InstructionBtn: MonoBehaviour {
public Texture2D normalTexture;
public Texture2D rollOverTexture;
public AudioClip beep;
public string levelToLoad;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter() {
guiTexture.texture = rollOverTexture;
//yield return new WaitForSeconds(.01); }
void OnMouseExit() {
guiTexture.texture = normalTexture;
}
IEnumerator OnMouseUp() {
audio.PlayOneShot(beep);
//StartCoroutine(Yielding(.35f));
yield return new WaitForSeconds(.35f);
Application.LoadLevel(levelToLoad);
}
private IEnumerator Yielding(float sec)
{
yield return new WaitForSeconds(sec);
}
private IEnumerator Yielding1(float sec)
{
yield return new WaitForSeconds(sec);
}
}
Thanks, Jeff
Answer by Mike 3 · Jul 27, 2010 at 07:31 PM
That actually looks perfect to me - absolutely sure that it's this script that is the problem? If so, have you tried reimporting it?
The RequireComponent attribute up at the top should be adding the audio source automatically
I am not sure what you mean by reimporting. However, the code was short so I deleted the script and created a new script with the same code and now it works. Hmmm...that is weird?!?!?
Reimport is right click script > reimport. But no, not weird - recreating the script would do the same thing.
Answer by Kristen · Jul 27, 2010 at 07:32 PM
In your code you wrote :
[RequireComponent(typeof(AudioSource))]
And so that means the object that this code is attached to must have a component of an audio source as well in order for it to work.
That tells unity that one is required, so it needs to be added by the engine when the script is initialized if one doesn't already exist
Answer by roberto_sc · Mar 13, 2017 at 05:56 AM
It adds the required component when you attach the script to the object, so if you attach it before you wrote the [RequireComponent]
bit, it won't work.
You'd have to reset the script, or remove it and add again.
Or, you can stop using the RequiredComponent
attribute and add it when using:
var audioSource = GetComponent<AudioSource>();
if(audioSource == null)
audioSource == gameObject.AddComponent<AudioSource>();
Please notice this is not the same thing, since it adds when using, if necessary. An alternative is to add it in the Start
method, so it would have a similar behaviour as RequiredComponent
.
Your answer

Follow this Question
Related Questions
Null component is not null? 1 Answer
Loading Audio Problems 0 Answers
"There is no 'Rigidbody' attached to..." but there is! 1 Answer