public override void update()
    {
        if (playerData != null)
        {
            if (playerData.health <= 0)
            {
                //gm.switchState(new GameOverState(gm));
            }
            if (playCanvas == null)
            {
                getPlayCanvas();
            }
            else
            {
                //****************HEALTH****************
                if (hp != null)
                {
                    hp.Update();
                }
                else
                {
                    hp = HP.GetInstance;
                }
                //****************ITEMS****************
                for (int i = 0; i < lastValues.Length; i++)
                {
                    if (lastValues[i] != playerData.items[i])
                    {
                        if (itemIcons[i] == null)
                        {
                            if (iconPrefab == null)
                            {
                                iconPrefab = Resources.Load <GameObject>("Icons/ItemImage");
                            }
                            itemIcons[i] = GameObject.Instantiate(iconPrefab, playCanvas.transform).GetComponent <Image>();
                            Sprite sprite = Resources.Load <Sprite>("Icons/" + System.Enum.GetName(typeof(ItemType), i));
                            if (sprite != null)
                            {
                                itemIcons[i].sprite = sprite;
                            }
                            itemIcons[i].rectTransform.Translate(new Vector3(-10, -20));
                            itemIcons[i].rectTransform.sizeDelta = itemIcons[i].rectTransform.sizeDelta * 3;
                            iconOffset       -= 60;
                            playerData.hasKey = true;
                        }
                        lastValues[i] = playerData.items[i];
                        //itemIcons[i].GetComponentInChildren<Text>().text = "x" + playerData.items[i];
                    }
                }
                //****************NOTES****************
                if (notesBg == null)
                {
                    notesBg = playCanvas.transform.Find("UINotes").GetComponent <Image>();
                }
                if (Input.GetButton("PlayMelody") || InputExtender.GetAxisDown("PlayMelody Trigger"))
                {
                    CanvasGroup cg = notesBg.GetComponent <CanvasGroup>();
                    cg.alpha = 1;
                    if (notes == null)
                    {
                        notes = new Image[5];
                        if (notesBg.transform.childCount == 0)
                        {
                            //TODO: Generate images
                        }
                        else
                        {
                            for (int i = 1; i <= notes.Length; i++)
                            {
                                notes[i - 1] = notesBg.transform.Find("Note" + i).GetComponent <Image>();
                            }
                        }
                    }
                    if (playerData.melodyData.PlayedNotes.Count > 0)
                    {
                        int i = 0;
                        foreach (Note n in playerData.melodyData.PlayedNotes)
                        {
                            Color c = notes[i].color;
                            //TODO: Change image sprite
                            Sprite toSprite = notes[i].sprite;
                            switch (n.noteID)
                            {
                            case Note.NoteID.G:
                                if (gm.notes[0] != null)
                                {
                                    toSprite = gm.notes[0];
                                }
                                break;

                            case Note.NoteID.A:
                                if (gm.notes[1] != null)
                                {
                                    toSprite = gm.notes[1];
                                }
                                break;

                            case Note.NoteID.B:
                                if (gm.notes[2] != null)
                                {
                                    toSprite = gm.notes[2];
                                }
                                break;

                            case Note.NoteID.C:
                                if (gm.notes[3] != null)
                                {
                                    toSprite = gm.notes[3];
                                }
                                break;

                            case Note.NoteID.D:
                                if (gm.notes[4] != null)
                                {
                                    toSprite = gm.notes[4];
                                }
                                break;

                            case Note.NoteID.E:
                                if (gm.notes[5] != null)
                                {
                                    toSprite = gm.notes[5];
                                }
                                break;

                            case Note.NoteID.Fplus:
                                if (gm.notes[6] != null)
                                {
                                    toSprite = gm.notes[6];
                                }
                                break;

                            case Note.NoteID.g8va:
                                if (gm.notes[7] != null)
                                {
                                    toSprite = gm.notes[7];
                                }
                                break;

                            default:
                                break;
                            }
                            notes[i].sprite = toSprite;
                            c.a             = 1;
                            notes[i].color  = c;
                            i++;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < notes.Length; i++)
                        {
                            Color c = notes[i].color;
                            c.a            = 0;
                            notes[i].color = c;
                        }
                    }
                }
                else
                {
                    CanvasGroup cg = notesBg.GetComponent <CanvasGroup>();
                    cg.interactable = false;
                    cg.alpha        = 0;
                }
            }
        }
        else
        {
            playerData = PlayerData.player;
            if (lastValues == null)
            {
                lastValues = new int[playerData.items.Length];
            }
            if (itemIcons == null)
            {
                itemIcons = new Image[lastValues.Length];
            }
        }

        if (Input.GetButtonDown("Cancel"))
        {
            gm.switchState(new PauseState(gm));
        }
    }
    public override void Act(StateController controller)
    {
        PlayerData data = (PlayerData)controller.data;

        PlayerData.MelodyData mData = data.melodyData;

        if (Input.GetButtonDown("PlayMelody") || InputExtender.GetAxisDown("PlayMelody Trigger"))
        {
            mData.playMelodyState = !mData.playMelodyState;

            // so you don't go to play melody when canceling a melody
            if (mData.currentMelody != null)
            {
                mData.playMelodyState = false;
                mData.playingFlute    = false;

                data.MelodyStoppedPlaying(mData.currentMelody);
                if (mData.currentMelody == Melody.MelodyID.MagicResistMelody)
                {
                    StoppedPlaying();
                }
                mData.currentMelody = null;
                data.melodyData.MelodyRange.enabled = false;
                controller.anim.SetBool("Channeling", false);
            }

            else if (mData.playMelodyState)   // on start reading input
            {
                AudioManager.FadeBGM();
                mData.playingFlute = true;
                data.songSheet.SetActive(true);

                if (mData.currentMelody != null)
                {
                    data.MelodyStoppedPlaying(mData.currentMelody);
                    if (mData.currentMelody == Melody.MelodyID.MagicResistMelody)
                    {
                        StoppedPlaying();
                    }
                }
                mData.currentMelody = null;
                data.melodyData.MelodyRange.enabled = false;
                controller.anim.SetBool("Channeling", true);
            }
            else   // on cancel playing
            {
                AudioManager.FadeBGMBackToNormal();
                mData.playingFlute = false;
                data.songSheet.SetActive(false);
                controller.anim.SetBool("Channeling", false);
            }
        }

        if (mData.playMelodyState)
        {
            // checks if a note is played
            Note notePlayed = null;
            //foreach (Note note in mData.Notes2) {
            //    if (note.CheckButton()) {
            //        mData.PlayedNotes.AddLast(note);
            //        notePlayed = note;
            //    }
            //}
            foreach (Note note in mData.Notes1)
            {
                if (note.CheckButton())
                {
                    mData.PlayedNotes.AddLast(note);
                    notePlayed = note;
                }
            }
            if (notePlayed != null)
            {
                AudioManager.PlayNote(notePlayed.audio);

                ParticleSystem m_fx = data.noteFX;
                ParticleSystem.TextureSheetAnimationModule m_anim = m_fx.textureSheetAnimation;
                m_anim.rowIndex = notePlayed.FXRowNumber;
                Instantiate(m_fx, new Vector2(data.transform.position.x, data.col.bounds.max.y), Quaternion.Euler(data.noteFX.transform.rotation.eulerAngles));
                m_fx.GetComponent <FXdestroyer>().hasPlayed = true;
            }

            while (mData.PlayedNotes.Count > mData.MaxSavedNotes)
            {
                mData.PlayedNotes.RemoveFirst();
            }

            // Checks id a melody have been played
            foreach (Melody melody in mData.melodies)
            {
                if (melody.CheckMelody(mData.PlayedNotes))
                {
                    mData.currentMelody = melody.melodyID;

                    if (mData.currentMelody == Melody.MelodyID.MagicResistMelody)
                    {
                        if (PlayedMagicResist != null)
                        {
                            PlayedMagicResist();
                        }
                    }
                    mData.playMelodyState     = false;
                    mData.MelodyRange.enabled = true;

                    data.MelodyPlayed(melody.melodyID);

                    AudioManager.FadeBGMBackToNormal();
                    mData.PlayedNotes.Clear();
                    data.songSheet.SetActive(false);

                    break;
                }
            }
        }
    }