I’m trying to check for an audioSource on an object but I can’t seem to find the right syntax.
if(gameObject.tag==("fish") AudioSource)
{
audio.Play();
Destroy(gameObject);
}
I’m sure this is 101 type stuff, but it eludes me.
thanks,
B.
I’m assuming that your AudioSource is a component on the object… so something like
var audio = gameObject.GetComponent(AudioSource); // or just gameObject.AudioSource
if(audio)
{
...
}
untested code…
Appreciate your input Lefty.
But what I was trying to do was set up a test for a specific tag check if the object had an audio source. I just wasn’t sure how to write it. If I check for objects with tag (“fish”) that do not have an audio source component, it kicks out an error.
I guess I could require an audio source on all objects with tag == (“fish”):
[RequireComponent(typeof(AudioSource))]
but I’m not sure if that would make a performance difference, so my first choice was to test for tag if there were an audio source present.
Thanks,
B.