S
S
sulim0003332020-08-12 22:07:36
Unity
sulim000333, 2020-08-12 22:07:36

How to make "switching" between buttons?

5f343bfccc3c2883998803.png

Here, using an example, I made two buttons responsible for the sound (background music in the game).
The task is such that when you press the first button, and then the second, the first button stops "acting", and after you press the first button back, the second one stops working, that is, to put it more simply - switching.
I tried to do this with my shit code using two bool variables, but the sounds still stack on top of each other instead of switching from one to the other.
I'm waiting for your help :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GFX Data, 2020-08-13
@sulim000333

You don't have separate AudioSource components. Better drag them to your component's inspector.

public AudioSource as1;
public AudioSource as2;

void Start()
{
    as1.Play();
}

public void Click1()
{
      as2.Stop();
      as1.Play();
}

public void Click2()
{
      as1.Stop();
      as2.Play();
}

G
GavriKos, 2020-08-13
@GavriKos

There are two approaches to this issue. As I understand it, one button is on, the second is off. Analog - RadioButton, aka Toggle.
The first is to separate the data and the visual. You should have one variable, two handlers (one for each button), and some kind of thing that will change the states of the buttons depending on the variable.
The second is to use the ready-made Toggle component.
No one will understand your shitty code in the form of a SCREEN.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question