Hi Everyone.
In my scene there are loads of GameObjects called Note each tagged as “note”, and another GameObject, let’s call it “reader”, that reads and then hopefully plays the note, the pitch of the note depends on what animation state the note is currently in, when the two GameObjects collide.
The problem is “reader” disregards what animation state the note is in, except for one note, every other note that collides with reader plays the same pitch note as that one note, how do I get reader to recognise the animation state on a note by note basis? I’ve tried messing around with FindObjectsWithTag, but honestly I’m quite new and don’t really understand how to use it. Thanks for any help.
My Code;
public class InstrumentController : MonoBehaviour {
private Animator noteanimator;
private GameObject note;
private GameObject[] notes;
private AudioSource source;
public AudioClip clipA;
public AudioClip clipASharp;
public AudioClip clipB;
public AudioClip clipC;
public AudioClip clipCSharp;
public AudioClip clipD;
public AudioClip clipDSharp;
public AudioClip clipE;
public AudioClip clipF;
public AudioClip clipFSharp;
public AudioClip clipG;
public AudioClip clipGSharp;
void Start ()
{
notes = GameObject.FindGameObjectsWithTag ("note");
foreach (GameObject note in notes)
{
noteanimator = note.GetComponent<Animator> ();
}
source = GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Rednote"))
{
source.PlayOneShot(clipB, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Bluenote"))
{
source.PlayOneShot(clipC, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Bluepurplenote"))
{
source.PlayOneShot(clipCSharp, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Purplenote"))
{
source.PlayOneShot(clipD, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Greypurplenote"))
{
source.PlayOneShot(clipDSharp, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Greynote"))
{
source.PlayOneShot(clipE, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Yellownote"))
{
source.PlayOneShot(clipF, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Yellowgreennote"))
{
source.PlayOneShot(clipFSharp, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Greennote"))
{
source.PlayOneShot(clipG, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Greenorangenote"))
{
source.PlayOneShot(clipGSharp, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Orangenote"))
{
source.PlayOneShot(clipA, 1f);
}
if (col.gameObject.tag == "note" && noteanimator.GetCurrentAnimatorStateInfo(0).IsName ("Pinkorangenote"))
{
source.PlayOneShot(clipASharp, 1f);
}
}
}