• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Image Target Recognizer‏ 0 Answers

How to implement voice overs linked with buttons? 0 Answers

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

Someone please help me with combination lock 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges