Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Benetto · May 28, 2016 at 06:37 PM · c#unity 5buttonaudiovuforia

How to use one button to play different audio for different Targets?

Hi I have a problem. Here is description. I use Vuforia library for Unity.

I am going to recognize few imageTargets and I would like to play sound with onClick(button). This button is on canvas and it is always on top during the app lifetime. So You can see it always. Behind this button You can see camera view and recognize markers.

F.e I have two markers: Dog and Cow. Dog has assigned audio - bark. Cow has assigned audio - muu.

When I recognize Cow -> click the button and it should give me muu sound, but when I recognize Dog, the same button, when clicked should give me bark sound. Here is a problem. I cannot resolve it. I think I should write a script for this button to play sound onClick for appropriate marker, but I do not know how to tell button that now I can see Cow (play muu onClick) and another time I can see Dog (play bark onClick). To sum up - How to give button info which audiofile should it use.

I made a script which plays sound when image is recognized, but I would like to do it with button.

Here: GetComponent<AudioSource>().Play(); It plays audio which is assigned to marker, but... It starts when camera recognize it (and without button). I would like to allow play proper sounds for Cow and Dog using only one, the same button. Below is full script:

 using UnityEngine;
 using Vuforia;
 
 public class TargetDogPlayAudio : MonoBehaviour,
                                             ITrackableEventHandler
 {
     public AudioClip DogSound;
     private TrackableBehaviour mTrackableBehaviour;
 
     void Start()
     {
         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
         if (mTrackableBehaviour)
         {
             mTrackableBehaviour.RegisterTrackableEventHandler(this);
         }
     }
 
     public void OnTrackableStateChanged(
                                     TrackableBehaviour.Status previousStatus,
                                     TrackableBehaviour.Status newStatus)
     {
         if (newStatus == TrackableBehaviour.Status.DETECTED ||
             newStatus == TrackableBehaviour.Status.TRACKED ||
             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
         {
             // Play audio when target is found
             GetComponent<AudioSource>().Play();
         }
         else
         {
             // Stop audio when target is lost
             GetComponent<AudioSource>().Stop();
         }
     }
 }
 

If something is not clear enough - let me know and I will write it better or again.

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need help with Mute Button 1 Answer

Need help making AR Text recognition more interactive 0 Answers

Vuforia Text Recognition how to add additional words through script? 0 Answers

Add an Icon next to a text inside a button Unity 5.6 1 Answer

Can't access functions on my UI button 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