• 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 Aappo · Oct 04, 2016 at 05:12 AM · audioaudiosourceaudioclipplayoneshotaudio.playoneshot

Why PlayOneShot is not working? C#

I have tried everything, but I can't find why. Here is the script:

 public AudioClip clip;
 private AudioSource fx;
 GameObject music;

 void Awake() {
     fx = GameObject.Find("_GM").GetComponent<AudioSource>();
     music = GameObject.Find("_music");
 }

 IEnumerator ChangeLevel () {
         float fadeTime = GameObject.Find("_GM").GetComponent<Fading>().BeginFade(1);
         yield return new WaitForSeconds(fadeTime);
     SceneManager.LoadScene(2);
 }

 void OnTriggerEnter2D (Collider2D col) {
     if (col.gameObject.tag == "center") {
         StartCoroutine(ChangeLevel());
         fx.PlayOneShot(clip, 1f);
         Destroy(music);
     }
 }

}

Comment
Add comment · Show 11
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 Aappo · Oct 03, 2016 at 07:19 PM 0
Share

BTW its not playing at all, but audio.Play() works well, but I need playoneshot for my game!

avatar image Lilius Aappo · Oct 04, 2016 at 05:47 AM 0
Share

Do you have the AudioClip clip assigned to this script in the inspector, any errors? Because the AudioSource.Play() plays the one assigned to audiosource, I think.

avatar image Aappo Lilius · Oct 04, 2016 at 06:54 AM 0
Share

I have it there and no errors... I'm just confused.

Show more comments
avatar image tanoshimi · Oct 04, 2016 at 06:59 AM 0
Share

Is the trigger being fired?

avatar image Aappo tanoshimi · Oct 04, 2016 at 07:04 AM 0
Share

Both Trigger collider2D

avatar image tanoshimi Aappo · Oct 04, 2016 at 07:54 AM 0
Share

No - what I meant was - is the OnTriggerEnter callback in your code getting called? Does the music stop and scene change as expected?

avatar image Aappo · Oct 04, 2016 at 10:26 AM 0
Share

Yes it changes the scenes normally and destroys music so its strange.

avatar image tanoshimi Aappo · Oct 04, 2016 at 10:43 AM 0
Share

Is it a 3D sound? Where is the _G$$anonymous$$ object in relation to your AudioListener? Is it simply too far away to hear?

avatar image Inok · Oct 04, 2016 at 11:28 AM 0
Share

Not use Awake for things like GameObject.Find because object can not exist at those moment, use awake for getcomponent on this object not other, so try move gameObject.Find to Start ins$$anonymous$$d of Awake. Not sure this is core of your problem but can be.

avatar image Bonfire-Boy · Oct 04, 2016 at 09:06 PM 0
Share

Could it just be that the scene change is happening too quickly? What value is being returned for fadeTime at line 11?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aappo · Oct 04, 2016 at 08:51 PM

Nothing worked :( if it is a bug?

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 andzq · Oct 14, 2016 at 09:50 PM 0
Share

your code does work without any problems, given that your

  void Awake() {
      fx = GameObject.Find("_G$$anonymous$$").GetComponent<AudioSource>();
      music = GameObject.Find("_music");
  }

DOES find something and the correct clip was assigned.

have you tested these already? :

  • checked, if the tag "center" is assigned to the object you plan colliding with

  • call fx.PlayOneShot(clip, 1f) before starting the coroutine

  • comment out the coroutine part completely

  • comment out Destroy(music)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer

Need help with AudioSources 1 Answer

2d PlayAudioOneShot inconsistent behavior 0 Answers

Can't get PlayOneShot() or Play() to work 1 Answer

2nd Audio Clip Not Playing 0 Answers


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