Некорректное воспроизведение звука

Программирование на Юнити.

Некорректное воспроизведение звука

Сообщение MarfilDrow 12 мар 2022, 07:39

Суть скрипта в том, что генерируется случайное число sh1. По нажатии на стрелку вверх переменная x1 увеличивается и воспроизводит звук. но как только x1 станет равно sh1 должен воспроизводиться другой звук, но независимо от этого постоянно воспроизводится первый.

Синтаксис:
Используется csharp
using UnityEngine;
 
public class AudioManager : MonoBehaviour {
 

    public int sh1;
    public int sh2;
    public int sh3;
    public int sh4;
    public int sh5;
    public int sh6;
    public int x1;
    public int x2;
    public int x3;
    public int x4;
    public int x5;
    public int x6;
    public int s;
    public AudioClip[] audioClips;
 
    public static AudioManager instance;
 
    private AudioSource player;
 
    private void Awake ()
    {
        player = GetComponent<AudioSource> ();
    }
 


    void Start ()
    {

        int sh1 = Random.Range(2,6);
        int sh2 = Random.Range(2,6);
        int sh3 = Random.Range(2,6);
        int sh4 = Random.Range(2,6);
        int sh5 = Random.Range(2,6);
        int sh6 = Random.Range(2,6);      

        Debug.Log(sh1);
        Debug.Log(sh2);
        Debug.Log(sh3);
        Debug.Log(sh4);
        Debug.Log(sh5);
        Debug.Log(sh6);

        int x1 = 0;
        int x2 = 0;
        int x3 = 0;
        int x4 = 0;
        int x5 = 0;
        int x6 = 0;
        int s = 0;

        if (instance == null) instance = this;
 
        DontDestroyOnLoad (gameObject);
    }
   
 
    public void playSound(AudioClip clip)
    {
        player.clip = clip;
        player.Play ();
    }
 
    public static AudioClip[] GetClips ()
    {
        return instance.audioClips;
    }
 
 
 
    public static void PlaySound(AudioClip clip)
    {
        instance.playSound (clip);
    }

            private void Update ()                
    {

    Debug.Log(x1);


        if (s == 0)
        {
            if (Input.GetKeyDown (KeyCode.UpArrow))
        {
            x1 = x1 + 1;
            if (x1 == sh1)
            {
                 if (AudioManager.GetClips ().Length > 1)
            {
                AudioManager.PlaySound (AudioManager.GetClips () [1]);
            }
            }
            else
            if (AudioManager.GetClips ().Length > 0)
            {
                AudioManager.PlaySound (AudioManager.GetClips ()[0]);
            }
        }
        }

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

        }
 
        if (Input.GetKeyDown (KeyCode.D))
        {
            if (AudioManager.GetClips ().Length > 2)
            {
                AudioManager.PlaySound (AudioManager.GetClips () [2]);
            }
        }
    }
}
 
MarfilDrow
UNец
 
Сообщения: 1
Зарегистрирован: 12 мар 2022, 07:32

Re: Некорректное воспроизведение звука

Сообщение 1max1 12 мар 2022, 15:53

Добавь логи в блоки if (x1 == sh1) else.
Аватара пользователя
1max1
Адепт
 
Сообщения: 5505
Зарегистрирован: 28 июн 2017, 10:51


Вернуться в Скрипты

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 11