S
S
sulim0003332020-08-10 20:31:48
Unity
sulim000333, 2020-08-10 20:31:48

How can I make it so that the first time the button is pressed, the sound is played, and the next time the button is pressed, the sound is turned off? Is there a simple primitive option?

5f3183bb0c50e913808002.png
I'm still quite new to this, in gamedev.
I looked for videos on youtube, but I didn't find anything there.
So I tried to implement this mechanism myself, but I don’t know what else to add and whether I’m eating correctly))
5f3183c293d36882258143.png
But I think you understand the meaning and purpose.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zZaKko, 2020-08-10
@sulim000333

Declare a bool variable, which, when pressed, will change to the opposite value and relative to what value the music will / will not play.

bool tor;//переменная

public boid fucking_click(){//метод клика
tor = !tor;//противоположное значение

if(tor)
GetComponent<AudioSource>().play();//играть
else
GetComponent<AudioSource>().stop();//стоп
}

T
Timur Pokrovsky, 2020-08-10
@Makaroshka007

https://docs.unity3d.com/ScriptReference/AudioSour...

G
GFX Data, 2020-08-10
@ShockWave2048

var audio = GetComponent<AudioSource>();

if (audio.isPlaying) audio.Play(); else audio.Stop();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question