Attaching sound to ImageTarget issues
Hi,
I am trying to attache sound to my ImageTarget so that the sound only plays when the target is located (and stops when the target is lost). I have searched online and found some tutorials (https://developer.vuforia.com/forum/faq/unity-how-can-i-play-audio-when-targets-get-detected) but I am still having compiler issues with the script I am using (sorry, I am not a developer and this is rather hard for me to understand). My errors and script below - any ideas on how to have this fixed would be very much appreciated!
The errors are;
audio.Play();
error: UnityEngine.Component' does not contain a definition for Play' and no extension method
Play' of type UnityEngine.Component' could be found. Are you missing
UnityEngine.Playables' using directive?
and
audio.Stop();
error: UnityEngine.Component' does not contain a definition for Stop' and no extension method
Stop' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?
Full script below;
using Vuforia; using UnityEngine;
public class ImageTargetPlayAudio : MonoBehaviour, ITrackableEventHandler { 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
audio.Play();
}
else
{
// Stop audio when target is lost
audio.Stop();
}
}
}
Your answer
Follow this Question
Related Questions
OnCollisonEnter2D Not Firing after checking collider 1 Answer
Mathf.Lerp Time problems. 1 Answer
Why does unity not see monobehaviour? 1 Answer
Performance peaks in ScriptRunBehaviourUpdate 1 Answer
Mathf.Lerp not working 0 Answers