-2
\$\begingroup\$

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 :\

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.