# Audio Source object reference not set to instance of an object in Unity.

I have the following script:

public class soundcontrol : MonoBehaviour {
AudioSource theaudio ;
public AudioClip clip1;
public AudioClip clip2;
public AudioClip clip3;
AudioClip[] cliparray;
int currentclip = -1;
int maxclip = 0;
bool isclip1null = false;
bool isclip2null = false;
bool isclip3null = false;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
maxclip = 0;
if (clip1 != null) {
isclip1null = true;
cliparray [maxclip] = clip1;
maxclip = maxclip + 1;
}
if (clip2 != null) {
isclip2null = true;
cliparray [maxclip] = clip2;
maxclip = maxclip + 1;
}
if (clip3 != null) {
isclip3null = true;
cliparray [maxclip] = clip3;
maxclip = maxclip + 1;
}
if (!theaudio.isPlaying) { // the error is here
if (!((maxclip - 1) == -1)) {
currentclip = currentclip + 1;
if (currentclip > maxclip) {
currentclip = 0;
}
if (!((maxclip - 1) == -1)) {
theaudio.PlayOneShot (cliparray [currentclip]);
}
}
}
if ((maxclip - 1) == -1) {
Debug.Log (" no clip attached ");
}
}
}


The error was at theaudio.isPlaying checking, the method I use is the same as what's in the documentation.

So, why is this thing happening? The documentation is just too bad to be understood (they can use that magic while I can't).

I also have audio source attached.

I'm a beginner with Unity so before you guys downvote please help me solve my problem first, and I'm sorry if this has already been asked somewhere, Google just seems to not give me the answer I want :\

• i wrongly put some part but that not solve the problem , editing post . – user6668201 Jan 16 '17 at 14:41
• You don't seem to initialize cliparray in any way... – Alexandre Vaillancourt Jan 16 '17 at 14:43
• @AlexandreVaillancourt , there is already part where its checked ( also not giving error in the log ) , the error i asking now is only audio source – user6668201 Jan 16 '17 at 14:44
• You don't seem to initialize theaudio in any way... – Alexandre Vaillancourt Jan 16 '17 at 14:50
• @AlexandreVaillancourt i already said ... in the documentation its work , then how can i initialize it ? – user6668201 Jan 16 '17 at 14:51

## 1 Answer

The error "object not set to an instance of an object" means that you're trying to use an object that is null (NullReferenceException). When you get that error, the first thing to do is to isolate which object/access is generating the error, then to trace back if that object is effectively initialized, and then make sure it's properly initialized before using it.

There is a more complete explanation on StackOverflow.

In your specific case, you need to initialize the audioSource object before you can use it.

The documentation about the method you're trying to use is quite explicit on how you do that: you set up the audioSource in the start function by getting the appropriate component.

// When the audio component has stopped playing, play otherClip
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public AudioClip otherClip;
AudioSource audio;

void Start() {
audio = GetComponent<AudioSource>();
}

void Update() {
if (!audio.isPlaying) {
audio.clip = otherClip;
audio.Play();
}
}
}

• can i use this.gameObject.GetComponent<AudioSource>() ? – user6668201 Jan 16 '17 at 14:54
• @user6668201 Why would you want to do that? – Alexandre Vaillancourt Jan 16 '17 at 14:55
• for different object – user6668201 Jan 16 '17 at 14:56
• @user6668201 I've never tried, but I guess you could. Not sure about the exact syntax, though. – Alexandre Vaillancourt Jan 16 '17 at 14:57
• i mean , i only need to drag and drop the script whenever i want to create new object with different clip – user6668201 Jan 16 '17 at 14:57