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 abhishekabz004 · May 31, 2015 at 07:05 AM · c#gameobjectsound

Why does sound attached to gameobject is not playing at desired time?

Hey! I am having an animator gameobject to which I have attached a sound source. I need the audio to be played when I click my gameobject. Here is my code.

public class destroy : MonoBehaviour {

 private AudioSource source;
 
 void Update () {
 
 }
 void OnMouseDown(){
     
     source = GetComponent<AudioSource>();
     source.Play ();
     
     //source.PlayOneShot ((AudioClip)Resources.Load("BOING_")); 
      }

}

The sound is being played. But the problem is it is getting played well before I click them. To be more precise its being played when the game starts. Please help me in solving this. Thanks in advance!! :)

Comment
Add comment · Show 5
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 screenname_taken · May 31, 2015 at 07:54 AM 1
Share

check the audio source object if the Play on awake box is ticked.

avatar image abhishekabz004 · May 31, 2015 at 03:36 PM 0
Share

Oh. Thanks for pointing out that :) But still, the sound would not play when I am clicking down on the gameobject. How to solve that one.?

avatar image Sprawl · May 31, 2015 at 05:14 PM 0
Share

Place a Debug.Log("clicked") in your On$$anonymous$$ouseDown Function to see if it is even called.

avatar image screenname_taken · May 31, 2015 at 05:33 PM 1
Share

Does your object have a collider? On$$anonymous$$ouseDown needs one if you are not doing a GUIElement interaction.

avatar image abhishekabz004 · May 31, 2015 at 05:42 PM 0
Share

Yes it do have a collider. Btw. This code is attached to a prefab. This prefab is instantiated multiple times as gameobjects. Just in case this information would lead you to help me. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by error031 · May 31, 2015 at 03:49 PM

Thats because OnMouseDown is called only when you are pressing mouse button. You should do something like this:

 private AudioSource source;
 
  void Update () {
  
  }
  void OnMouseDown(){
      
      source = GetComponent<AudioSource>();
      PlaySound();
 }

  void PlaySound(){
      source.Play ();
 }
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 abhishekabz004 · May 31, 2015 at 05:18 PM 0
Share

Thanks for replying :) That is what I need. I just want the sound to be played when the mouse is clicked on the game object. That would make code a proper one right? Sorry. I might be wrong. Please guide me by explaining ur statement. :) Btw, even after trying your code it is not working.

avatar image Sprawl · May 31, 2015 at 05:23 PM 1
Share

That is the exact same code as the one in the question but the Source.Play() is in a function. If On$$anonymous$$ouseDown() is not called, then PlaySound() will not work either.

@abhishekabz004 , Try putting a Debug.Log into your On$$anonymous$$ouseDown() Function to see if it is called.

avatar image abhishekabz004 · May 31, 2015 at 05:32 PM 0
Share

I have done that before. Its getting called. There is no problem in it. Even when I tried the PlaySound() method, it got called either. But still the sound is not playing.

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

22 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I move one of many instantiated gameobjects ONLY if it's currently selected in a list? 1 Answer

Making changes to a terrian in game 1 Answer

C# 2d Instantiate object randomly around gameobject 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