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 Brent Gilson · Jun 20, 2013 at 04:41 PM · audioclick

Clicking Game Object Scrpit

I'm trying to create a script where if I click on a certain game object, it plays an audio source. So far I got it to play the audio source whenever I click the mouse button but I only want it to play if I click a certain game object.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by AntiLunchBox · Jun 20, 2013 at 05:33 PM

Depends what platform you are planning to use. In both situations you have to throw a collider on the GameObject.

If you aren't building to mobile, only platforms that use mouse clicks -- you could just throw a script that has an OnMouseDown function on it that calls audio.Play();

For a more general approach, you want to use Raycasting. Here is an example:

 if ( Input.GetMouseButtonDown(0)){
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

     if (Physics.Raycast (ray, hit, 100.0))
         if(hit.collider.transform == target)
             target.audio.Play();
 }
   


Comment
Add comment · Show 5 · 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 AntiLunchBox · Jun 20, 2013 at 05:36 PM 0
Share

$$anonymous$$eep in $$anonymous$$d that for different platforms, the input is different. like Input.touches

avatar image AntiLunchBox · Jun 20, 2013 at 06:54 PM 0
Share

did that work???

avatar image Brent Gilson · Jun 21, 2013 at 04:14 PM 0
Share

It doesn't seem to be working for me. What variables do I need? And would I replace target with the Game Object?

avatar image AntiLunchBox · Jun 21, 2013 at 04:47 PM 0
Share

yes you would replace target with the gameobject that has the audiosource on it that you want to play

avatar image AntiLunchBox · Jun 21, 2013 at 04:49 PM 0
Share

also, you can just do Physics.Raycast(ray,hit)

If you look at the raycasting link i sent you, it says what the variables mean. the 100 gives it a maximum distance of 100. If the object is 100 away from the camera, it won't trigger.

avatar image
0

Answer by juliancruz · Jun 20, 2013 at 08:38 PM

Add collider component to object and reference the audio.

void OnMouseDown() { sound.Play(); }

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

16 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

Related Questions

How to play an audio one shot clip on a collision 1 Answer

Click to destroy object 2 Answers

Audio Help! 1 Answer

help sound on collision when key down 2 Answers

How to make audio play when passes a collider 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