D
D
dani220232021-10-07 19:55:41
C++ / C#
dani22023, 2021-10-07 19:55:41

How to solve the problem with playing sound through a key?

I wrote the code, I turn on the game, but there is no sound when I press the key. What to do?

public class sosjn : MonoBehaviour
{


    private AudioSource AudioNotes;

    [Header("Массив нот")]
    [SerializeField] private AudioClip[] Notes = new AudioClip[4];

    void Start()
    {
        AudioNotes = GetComponent<AudioSource>();
    }


    public void Update(int numberNotes)
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            AudioNotes.clip = Notes[numberNotes];
            AudioNotes.Play();
        }

    }

   


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Almost, 2021-10-08
@dani22023

Nothing happens because the void Update(int) method is not called every game frame.
Use the void Update() method - then the method will be correctly called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question