Search Unity

MissingComponentException: no 'AudioClip' even if i add it

Discussion in 'Audio & Video' started by leifgood, Nov 28, 2015.

  1. leifgood

    leifgood

    Joined:
    Nov 28, 2015
    Posts:
    12
    Hello,

    so i was trying to use the beat detector by allanpichardo:

    https://github.com/allanpichardo/Unity-Beat-Detection

    I wanted to understand what the program was doing, so i didnt simply copy-paste everything. I typed everything myself, and came across something with an audio Object. The following lines will show you what he wrote and what i had to write instead ( i guess ):

    AudioProcessor, line 81:
    Him: samplingRate = audio.clip.frequency;
    Me: samplingRate = GetComponent<AudioClip>().frequency;

    AudioProcessor, line 122:
    Him: if (audio.isPlaying) ...
    Me: if (GetComponent<AudioSource>().isPlaying) ...

    AudioProcessor, line 125:
    Him: audio.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
    Me: GetComponent<AudioSource>().GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);

    I thought this must be the new way to write it ( had the same problem with a tutorial on the internet few weeks ago with "rigidbody", untill i read you have to use GetComponent<rigidbody>() now ). It makes sense to me. So i added a AudioSource to my Main Camera, draged and droped my audiofile into the "AudioClip"-field and added the "Example"-script to an Object.

    Running the program throws 2 different Exceptions. Iam going to focus in the first one. The second one is probably caused by the first one, so iam not taking this any further.

    MissingComponentException: There is no 'AudioClip' attached to the "Main Camera" game object, but a script is trying to access it.

    Why cant the program find my AudioClip and how do i fix this issue?
     
    Last edited: Sep 10, 2018
  2. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    281
    Make sure that the AudioSource is added to the same object as the script. You could also make a public AudioSource field named "audio" and then drag/drop the one you added to your camera.
     
  3. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    GetComponent are only used for components. AudioClip is not one. If you're unsure whether something is a component, try adding it to a GameObject though the "Add Component" button in the inspector.

    You need to get the clip using GetComponent<AudioSource>().clip