Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by biotep1 · Sep 02, 2014 at 08:28 PM · audioclipfunction call

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!
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by kacyesp · Sep 02, 2014 at 08:44 PM

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();
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biotep1 · Sep 05, 2014 at 11:31 AM 0
Share

Thanks kacyesp, This solution is working as well! However Im wondering how doest it affect the performance if all my 20 - 30 Animal GameObjects will have an AudioSource on them?

avatar image biotep1 · Sep 05, 2014 at 11:35 AM 0
Share

...its a 2D game and doesn't really matter from which direction the sound is co$$anonymous$$g from so I'm not trying to make it stereo.

avatar image kacyesp · Sep 05, 2014 at 01:52 PM 0
Share

I don't know how performance heavy AudioSources, and I'm not sure many people do. But I do know that you have 2 options when it comes to this. The first option is what you're already doing. You can use an AudioSource for each game object, but obviously the space you use will increase. The 2nd option is you can pool AudioSources to play sounds, so you'd use like 1 to 4 AudioSources to play all the sounds you want. A single AudioSource can play multiple sounds simultaneously using PlayOneShot(). The problem with this option is that every time you call PlayOneShot(), a brand new AudioSource is created and then destroyed at the end of the clip. I would post another question asking what's a good balance of AudioSources to use. But I think if you're not worried about space, creating an AudioSource for each game object is the best option.

avatar image
0

Answer by smoggach · Sep 02, 2014 at 08:42 PM

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();

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biotep1 · Sep 05, 2014 at 11:27 AM 0
Share

Thanks smoggach for pointing out the mistake....I've converted the AudioClipPlayer to singleton and now it works. thanks again!

avatar image
0

Answer by kutetien · Sep 05, 2014 at 12:38 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by pintin · Sep 30, 2015 at 04:51 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where does streamed audio clip stored and removed? 0 Answers

Microphone Position goes larger than predetermined number of samples 0 Answers

Extract audio clip from VideoPlayer for real-time processing 0 Answers

Microphone capture different channels 0 Answers

How to manage player and enemies shooting sounds at the same time? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges