storing sounds in Array and calling PlayOneShot from other class

Hi Folks,

I have the following newbie problem and its getting frustrating so Im asking you in the hope someone has a hint:

I have two scripts: AudioClipPlayer is attached to an empty gameobject and has an array loaded up with AudioClips. The second scipt has been added multiple Sprite objects and it supposed to call the AudioPlay() function in the AudioClipPlayer whenever the sprite object receives a click.

Calling the AudioPlay() script internally within the class working fine - see the commented section in AudioClipPlayer:

   using UnityEngine;
    using System.Collections;
    
    public  class AudioClipPlayer : MonoBehaviour  {
    
        public  AudioClip[]  audioarray;
    
        public  void Start()
        {
            audioarray =  new AudioClip[]
            {
                Resources.Load("bark")   as AudioClip,
                Resources.Load("meow")  as AudioClip
            };
    
            //the following line of code successfully play an audioclip:
            //PlaySound();
        }
    
        public void PlaySound()
        {
            audio.PlayOneShot(audioarray[1]);
        }
    }

Here is the script ( stripped down version ) which is attached to the Sprite objects:

using UnityEngine;
    using System.Collections;
    
    public class AnimalScript : MonoBehaviour
    {
    	AudioClipPlayer audioClipPlayer = new AudioClipPlayer();
    
    	public Sprite newSprite; 
    
    	public void OnMouseDown()
    	{
    		audioClipPlayer.PlaySound();
    	}
    }

however calling the PlaySound() from this class fails with the following error message:

NullReferenceException
AudioClipPlayer.PlaySound () (at Assets/AudioClipPlayer.cs:49)
AnimalScript.OnMouseDown () (at Assets/AnimalScript.cs:39)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

Hope someone can give me a hint -
Thank you guys!

Add a constructor to your AudioClipPlayer and pass in the audio field.
There’s no need for AudioClipPlayer to implement MonoBehaviour.

Code with the constructor and without MonoBehaviour:

using UnityEngine;
using System.Collections;

public  class AudioClipPlayer {

    public  AudioClip[]  audioarray;
    public AudioSource audio;

    public AudioClipPlayer( AudioSource audio ) 
    {
        this.audio = audio;

        audioarray =  new AudioClip[]
        {
            Resources.Load("bark")   as AudioClip,
            Resources.Load("meow")  as AudioClip
        };
    }

    public void PlaySound()
    {
        audio.PlayOneShot(audioarray[1]);
    }
}

Code passing in the audio field:

using UnityEngine;
using System.Collections;

public class AnimalScript : MonoBehaviour
{
    AudioClipPlayer audioClipPlayer = new AudioClipPlayer( audio );

    public Sprite newSprite; 

    public void OnMouseDown()
    {
        audioClipPlayer.PlaySound();
    }
}

The problem is that you are instantiating a monobehavior. Monobehavior is only meant to go on script components. Because you instantiate it from another object’s Start it never receives a Start function of it’s own until the next frame.

I’d recommend using a Singleton.
In the Awake function of AudioClipPlayer:
DontDestroyOnLoad();

Then attach that script to it’s own global gameobject. Now it will exist in all the scenes.

However it’s still tricky to access so you’ll have to add a static instance variable.
public static AudioClipPlayer Instance;

Now back in the Awake function:
Instance = this;

Now from any of your scripts you can simply call:
AudioClipPlayer.Instance.PlaySound();

the sharing is necessary to thank you for this loinhs.kizi

1: http://www.kizicode.com | friv

Yes. you should seo and merketing. Whether you are an owner of a company with hundreds of employees or just own a small shop, business to succeed you need to have an effective marketing strategy and apply it frequently. However, this does not require you to spend too much money and you do not necessarily have to be a creative genius dora, kizi.