- Home /
There is no 'AudioSource' attached Error
I am getting this error but I have audio attached to it. I have deleted the component and even rebooted. Weird issue.
So here is the error: MissingComponentException: There is no 'AudioSource' attached to the "Input-Escape-MainMenu" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "Input-Escape-MainMenu". Or your script needs to check if the component is attached before using it. InputEscape+c__Iterator6.MoveNext () (at Assets/Extras Created/InputEscape.cs:47) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) InputEscape:Update() (at Assets/Extras Created/InputEscape.cs:40)
here is the code. It only gives the error after pressing the spacebar
using UnityEngine;
using System.Collections;
///////////////////////////////////////////////////////////////
public class InputEscape : MonoBehaviour
{
///////////////////////////////////////////////////////////////
public GameObject mainMenuBtn; // quit button
public GameObject nextBtn; // next button
public GameObject overHeadBtn; // overhead button
public GameObject effectNewBestScore;
new AudioSource audio;
public AudioClip menuSound51;
///////////////////////////////////////////////////////////////
void Awake()
{
audio = GetComponent<AudioSource>();
mainMenuBtn = GameObject.FindWithTag("MainMenuButton");
overHeadBtn = GameObject.FindWithTag("OverHeadButton");
effectNewBestScore = GameObject.FindWithTag("BestScore");
nextBtn = GameObject.FindWithTag("NextHoleButton");
}
///////////////////////////////////////////////////////////////
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
CanvasButtonsOnOff.fire1ButtonPressed = true;
mainMenuBtn.SetActive(false);
nextBtn.SetActive(false);
overHeadBtn.SetActive(false);
//destroy settings-1, settings-2.....courses
Destroy(GameObject.FindWithTag("settings-all"));
StartCoroutine(WaitForMainMenu2());
}
}
////////////////////////////////////////////
IEnumerator WaitForMainMenu2()
{
audio.PlayOneShot(menuSound51, 1f);
while (audio.isPlaying == true)
{
yield return null;
}
effectNewBestScore.SetActive(false);
Application.LoadLevel("MainMenu");
}
////////////////////////////////////////////
}
Now look at the pictures: 
Please insert Debug.Log(...) into your Awake() and check if audio == null in Awake?
1) if I run it from the editor in the actual scene, it works fine. If I start at the main menu and have it load the scene, it doesn't work. It gives the error.
2) here is console with
Debug.Log(audio);
Input-Escape-$$anonymous$$ain$$anonymous$$enu (UnityEngine.AudioSource) UnityEngine.Debug:Log(Object) InputEscape:Awake() (at Assets/Extras Created/InputEscape.cs:25) ==========
3)After putting the debug line it has worked fine 20 times in a row. A Unity bug maybe?
It only gives the error after pressing the spacebar
$$anonymous$$ay be you've pressed Spacebar earlier then Awake() finished?
Now I'm getting this related error. this was fine and just starting doing this. Some unity bug?
$$anonymous$$issingComponentException: There is no 'Rigidbody' attached to the "Ball-Generated(Clone)" game object, but a script is trying to access it. You probably need to add a Rigidbody to the game object "Ball-Generated(Clone)". Or your script needs to check if the component is attached before using it. UnityEngine.Rigidbody.get_velocity () (at C:/buildslave/unity/build/artifacts/generated/common/modules/DynamicsBindings.gen.cs:784) Arrow.Update () (at Assets/$$anonymous$$iniGolf/Scripts-1/Arrow.cs:18)
Here is picture showing I have rigidbody. 
and here is another picture of the game being played with the clone of the ball. There is a rigibody.

I added this to with a debug.log.
///////////////////////////////////////////////////////////////
void Awake()
{
Debug.Log("Rigidbody: " + GetComponent<Rigidbody>());
}
///////////////////////////////////////////////////////////////
and look at picture. The debug says there is a rigidbody but then it says its missing. But its clearly there

Answer by Gilles_aerts · Oct 09, 2015 at 06:18 PM
Hi try making a public or private audioSource variable instead of new audioScource
( private AudioSource audio ; )
Start(){ audio = getComponent(AudioSource) as AudioSource;
}
Answer by money4honey · Oct 12, 2016 at 03:53 PM
I was have this error too. In my case maybe it happened because I add some audio files to project folder through file system. I heard it may cause some bugs. But, then I re import all audios it solved the problem
Your answer