0

I am making a piano game/app in unity and I want to be able to record the players notes and then play it back, I found a way to do that with an ArrayList, but I activate recording by a KeyPress, I want to do it by clicking a UI Button in unity. I can't figure out how to do that. The other problem I have is that the game doesn't play the notes after it records, I want to be able to play the notes while I am recording and also if I am not recording. My Code is down below, thank you for any help

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NotePlay: MonoBehaviour
{


    Button record;
    public bool recordMode = false;
    Animator anim;
    public AudioClip noteA;
    public AudioClip noteB;
    public AudioClip noteC;
    public AudioClip noteD;
    public AudioClip noteE;
    public AudioClip noteF;
    public AudioClip noteG;
    public AudioSource audio;

    // Use this for initialization

    public ArrayList notes;

    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
        audio = GetComponent<AudioSource>();
        notes = new ArrayList();

    }

    void Playback()
    {
        print(notes.Count);
        for (int i = 0; i < notes.Count; i++)
        {

            char c = (char)notes[i];
            System.Threading.Thread.Sleep(1000);
            PlayNote(c);

        }


    }

    void PlayNote(char note)
    {

        if (note == 'a')
        {

            //anim.SetTrigger("A");
            GameObject.Find("Sphere_A").GetComponent<AudioSource>().PlayOneShot(noteA);
            GameObject.Find("Sphere_A").GetComponent<Animator>().SetTrigger("A");

        }
        if (note == 'b')
        {

            //anim.SetTrigger("B");
            GameObject.Find("Sphere_B").GetComponent<AudioSource>().PlayOneShot(noteB);
            GameObject.Find("Sphere_B").GetComponent<Animator>().SetTrigger("B");
            print("b");

        }
        if (note == 'c')
        {

            ///anim.SetTrigger("C");
            GameObject.Find("Sphere_C").GetComponent<AudioSource>().PlayOneShot(noteC);

        }
        if (note == 'd')
        {

            //anim.SetTrigger("D");
            GameObject.Find("Sphere_D").GetComponent<AudioSource>().PlayOneShot(noteD);

        }
        if (note == 'e')
        {

            //anim.SetTrigger("E");
            GameObject.Find("Sphere_E").GetComponent<AudioSource>().PlayOneShot(noteE);

        }
        else if (note == 'f')
        {

            // anim.SetTrigger("F");
            GameObject.Find("Sphere_F").GetComponent<AudioSource>().PlayOneShot(noteF);

        }
        else if (note == 'g')
        {

            //anim.SetTrigger("G");
            GameObject.Find("Sphere_G").GetComponent<AudioSource>().PlayOneShot(noteG);

        }


    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.R))
        {

            recordMode = !recordMode;


        }

        if (recordMode == true)
        {
            if (Input.GetKeyDown(KeyCode.A)) { notes.Add('a'); print("a"); }
            if (Input.GetKeyDown(KeyCode.B)) {notes.Add('b'); print("b"); }
            if (Input.GetKeyDown(KeyCode.C)) notes.Add('c');
            if (Input.GetKeyDown(KeyCode.D)) notes.Add('d');
            if (Input.GetKeyDown(KeyCode.E)) notes.Add('e');
            if (Input.GetKeyDown(KeyCode.F)) notes.Add('f');
            if (Input.GetKeyDown(KeyCode.G)) notes.Add('g');


        }
        else
        {

            if (Input.GetKeyDown(KeyCode.P))
            {

                Playback();

            }
        }

    }
    }
derHugo
  • 83,094
  • 9
  • 75
  • 115
deep
  • 11
  • 4

1 Answers1

0

For the first part of your question, you need to add a listener to your button. Add this to your Start() method:

record.OnClick.AddListener(()=>{ recordMode = !recordMode; });

This will make it so that when the button attached to the record field is pressed, the action is executed.

For the second part of your question you could call PlayNote() every time the note is recorded like so:

if (Input.GetKeyDown(KeyCode.G)){
 notes.Add('g');
 PlayNote('g');
}
Matthew Andrews
  • 437
  • 1
  • 5
  • 16