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 Pflobus · Dec 20, 2014 at 06:54 PM · audiosourceaudioclipaudio.playoneshot

Play Audio on Object click?

So earlier, I asked a question regarding creating a GUI on the click of an object. And I was helped out by @ahmedbenlakhdhar, @static_cast, and @Titirez5. I recently realized that I probably don't need the GUI though :/ I realized that I might need an audioclip. So, I tried to figure out how exactly it works, but I clearly was very confused.

So, here's my script. I am hoping to have it so that, rather than a texture, I get a variable for an AudioClip, and the there should be something about an AudioSource.... Anyway, here is what I have as of now:

using UnityEngine; using System.Collections;

public class Click : MonoBehaviour {

 public Texture tex;
 public GameObject MYOBJECT = null;
 //public float GUItrialone = GUI.Button(350, 350, (10, 20 "Try!"))
 bool        displayGUI = false;
 void    OnGUI()
 {
     if (!tex)
         Debug.LogError ("Really Dude? After all this, you forget a TEXTURE???");

     if (Input.GetMouseButtonUp(0))
     {
         Ray            ray = Camera.main.ViewportPointToRay(Camera.main.ScreenToViewportPoint(Input.mousePosition));
         RaycastHit    outinfo;
         
         if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
         {
             //TOUCH! Show GUI
             if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
             {
                 if(outinfo.transform.gameObject == MYOBJECT){
                     displayGUI = true;
                 }
             }

         }
     }
     if (displayGUI == true)
     {
         if(GUILayout.Button(tex))
             displayGUI = false;
         //if (GUILayout.Button (tex)) 
         //{
             //displayGUI = false;
         //}
     }


 }

}

Any help is greatly appreciated! Thank you!

EDIT:

So, here is a more updated script. My problem now is that I can't assign an AudioSource. Whenever I click to assign an AudioSource, and I look at either the Assets or the Scene tabs, it says there's none. Here it is:

using UnityEngine; using System.Collections;

public class ClickAudio : MonoBehaviour {

 public AudioSource Source;
 public AudioClip Clip;
 public GameObject MYOBJECT = null;
 bool        displayGUI = false;
 void    OnGUI()
 {
     
 
     
     if (Input.GetMouseButtonUp(0))
     {
         Ray            ray = Camera.main.ViewportPointToRay(Camera.main.ScreenToViewportPoint(Input.mousePosition));
         RaycastHit    outinfo;
         
         if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
         {
             //TOUCH! Show GUI
             if (Physics.Raycast(ray, out outinfo, Mathf.Infinity))
             {
                 if(outinfo.transform.gameObject == MYOBJECT){
                     displayGUI = true;
                 }
             }
             
         }
     }
     if (displayGUI == true)
     {

         Source.Play();
         
     }
     
     
 }

}

Again, any help is greatly appreciated!

Comment
Add comment · Show 21
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 meat5000 ♦ · Dec 20, 2014 at 03:41 PM 1
Share

What's your question?

avatar image Pflobus · Dec 20, 2014 at 03:49 PM 0
Share

Oh. Sorry, that was really unclear! $$anonymous$$y question is about how to add in an AudioClip rather than a GUI. But, there's a bunch of stuff like AudioSource and such, which overcomplicates it for me, so I needed help.

avatar image Pflobus · Dec 20, 2014 at 04:05 PM 0
Share

Oh yeah. I know I don't need a GUI. I just wanted to know how to add in the Audio stuff to my script.

avatar image meat5000 ♦ · Dec 20, 2014 at 04:35 PM 0
Share

http://answers.unity3d.com/questions/611832/on-touch-play-sound.html

avatar image meat5000 ♦ · Dec 20, 2014 at 05:50 PM 1
Share

NONE means there's no audioclips in your project. If you do actually have a clip, find it in the project explorer and drag-n-drop.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Dec 20, 2014 at 04:01 PM

Audio is componentised.

It means you need to add the component to an object you want to play audio. From here you add (and assign) audio clips. Once the clip is assigned you can play it.

Load your clips in to AudioClip, then use the Play function :)

The source determines where the sound comes from. For example if you put the source next to the listener, this is good for Game Music. You can have multiple sources, but one Listener.

You won't need GUI if you use RayCast to 'touch' an object. Note that a collider is important in this case.

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 flapy-monkey · Dec 24, 2014 at 05:52 PM

1.make a new script 2.void OnMouseDown() { //TODO:play sound? } 3.drag to your object.

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems combining Audio triggers, PlayOneShot and Audio Arrays 1 Answer

How to switch Audiosource without stacking by an input? 0 Answers

2nd Audio Clip Not Playing 0 Answers

Why PlayOneShot is not working? C# 1 Answer

Audio stops playing after releasing my Mouse Button and pressing another key 2 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