Search Unity

How to play audio every time I press the Button

Discussion in 'Scripting' started by FK22, Jun 2, 2019.

  1. FK22

    FK22

    Joined:
    Jul 7, 2017
    Posts:
    40
    Hi, I am trying to play 12 audios when I press the Button "play". I have saved those audios in 12 gameobjects in Unity and in the function start, I disable them. Then I shuffle them, so the sounds will be played randomly and I use coroutine to play a sound when the button is pressed. All works ok, but I hear only 11 sounds played. Even though I have declared 12 audios and I loop 12 times, I still get only 11 audios played. Any advice would be very helpful.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlaySounds : MonoBehaviour
    7. {
    8.  
    9.  
    10.     public Button Play;
    11.  
    12.     List<string> mylist = new List<string>();
    13.  
    14.     public GameObject _1;
    15.     public GameObject _2;
    16.     public GameObject _3;
    17.     public GameObject _4;
    18.     public GameObject _5;
    19.     public GameObject _6;
    20.     public GameObject _7;
    21.     public GameObject _8;
    22.     public GameObject _9;
    23.     public GameObject _10;
    24.     public GameObject _11;
    25.     public GameObject _12;
    26.  
    27.  
    28.     string selected;
    29.  
    30.     bool flag = false;
    31.  
    32.     void Start()
    33.     {
    34.  
    35.         _1.SetActive(false);
    36.         _2.SetActive(false);
    37.         _3.SetActive(false);
    38.         _4.SetActive(false);
    39.         _5.SetActive(false);
    40.         _6.SetActive(false);
    41.         _7.SetActive(false);
    42.         _8.SetActive(false);
    43.         _9.SetActive(false);
    44.         _10.SetActive(false);
    45.         _11.SetActive(false);
    46.         _12.SetActive(false);
    47.  
    48.         mylist.Add("1");
    49.         mylist.Add("2");
    50.         mylist.Add("3");
    51.         mylist.Add("4");
    52.         mylist.Add("5");
    53.         mylist.Add("6");
    54.         mylist.Add("7");
    55.         mylist.Add("8");
    56.         mylist.Add("9");
    57.         mylist.Add("10");
    58.         mylist.Add("11");
    59.         mylist.Add("12");
    60.  
    61.         Shuffle(mylist);
    62.  
    63.         StartCoroutine(DoThis());
    64.  
    65.  
    66.     }
    67.  
    68.     void PlaySoundsOnClick()
    69.     {
    70.  
    71.         flag = true;
    72.  
    73.      
    74.     }
    75.  
    76.  
    77.  
    78.     IEnumerator DoThis ()
    79.     {
    80.         for (int i = 0; i < 12; i++)
    81.         {
    82.  
    83.  
    84.  
    85.  
    86.             Play.onClick.AddListener(PlaySoundsOnClick);
    87.  
    88.             selected = mylist[i];
    89.  
    90.             if (flag == true && selected == "1")
    91.             {
    92.  
    93.                 _1.SetActive(true);
    94.  
    95.                 _2.SetActive(false);
    96.                 _3.SetActive(false);
    97.                 _4.SetActive(false);
    98.                 _5.SetActive(false);
    99.                 _6.SetActive(false);
    100.                 _7.SetActive(false);
    101.                 _8.SetActive(false);
    102.                 _9.SetActive(false);
    103.                 _10.SetActive(false);
    104.                 _11.SetActive(false);
    105.                 _12.SetActive(false);
    106.  
    107.      
    108.  
    109.             }
    110.             if (flag == true && selected == "2")
    111.             {
    112.  
    113.                 _2.SetActive(true);
    114.  
    115.                 _1.SetActive(false);
    116.                 _3.SetActive(false);
    117.                 _4.SetActive(false);
    118.                 _5.SetActive(false);
    119.                 _6.SetActive(false);
    120.                 _7.SetActive(false);
    121.                 _8.SetActive(false);
    122.                 _9.SetActive(false);
    123.                 _10.SetActive(false);
    124.                 _11.SetActive(false);
    125.                 _12.SetActive(false);
    126.  
    127.            
    128.  
    129.             }
    130.             if (flag == true && selected == "3")
    131.             {
    132.  
    133.                 _3.SetActive(true);
    134.  
    135.                 _1.SetActive(false);
    136.                 _2.SetActive(false);
    137.                 _4.SetActive(false);
    138.                 _5.SetActive(false);
    139.                 _6.SetActive(false);
    140.                 _7.SetActive(false);
    141.                 _8.SetActive(false);
    142.                 _9.SetActive(false);
    143.                 _10.SetActive(false);
    144.                 _11.SetActive(false);
    145.                 _12.SetActive(false);
    146.  
    147.  
    148.  
    149.             }
    150.             if (flag == true && selected == "4")
    151.             {
    152.  
    153.                 _4.SetActive(true);
    154.  
    155.                 _1.SetActive(false);
    156.                 _2.SetActive(false);
    157.                 _3.SetActive(false);
    158.                 _5.SetActive(false);
    159.                 _6.SetActive(false);
    160.                 _7.SetActive(false);
    161.                 _8.SetActive(false);
    162.                 _9.SetActive(false);
    163.                 _10.SetActive(false);
    164.                 _11.SetActive(false);
    165.                 _12.SetActive(false);
    166.  
    167.  
    168.  
    169.             }
    170.             if (flag == true && selected == "5")
    171.             {
    172.  
    173.                 _5.SetActive(true);
    174.  
    175.                 _1.SetActive(false);
    176.                 _2.SetActive(false);
    177.                 _3.SetActive(false);
    178.                 _4.SetActive(false);
    179.                 _6.SetActive(false);
    180.                 _7.SetActive(false);
    181.                 _8.SetActive(false);
    182.                 _9.SetActive(false);
    183.                 _10.SetActive(false);
    184.                 _11.SetActive(false);
    185.                 _12.SetActive(false);
    186.  
    187.          
    188.  
    189.             }
    190.             if (flag == true && selected == "6")
    191.             {
    192.  
    193.                 _6.SetActive(true);
    194.  
    195.                 _1.SetActive(false);
    196.                 _2.SetActive(false);
    197.                 _3.SetActive(false);
    198.                 _4.SetActive(false);
    199.                 _5.SetActive(false);
    200.                 _7.SetActive(false);
    201.                 _8.SetActive(false);
    202.                 _9.SetActive(false);
    203.                 _10.SetActive(false);
    204.                 _11.SetActive(false);
    205.                 _12.SetActive(false);
    206.  
    207.          
    208.  
    209.             }
    210.             if (flag == true && selected == "7")
    211.             {
    212.  
    213.                 _7.SetActive(true);
    214.  
    215.                 _1.SetActive(false);
    216.                 _2.SetActive(false);
    217.                 _3.SetActive(false);
    218.                 _4.SetActive(false);
    219.                 _5.SetActive(false);
    220.                 _6.SetActive(false);
    221.                 _8.SetActive(false);
    222.                 _9.SetActive(false);
    223.                 _10.SetActive(false);
    224.                 _11.SetActive(false);
    225.                 _12.SetActive(false);
    226.  
    227.  
    228.             }
    229.             if (flag == true && selected == "8")
    230.             {
    231.  
    232.                 _8.SetActive(true);
    233.  
    234.                 _1.SetActive(false);
    235.                 _2.SetActive(false);
    236.                 _3.SetActive(false);
    237.                 _4.SetActive(false);
    238.                 _5.SetActive(false);
    239.                 _6.SetActive(false);
    240.                 _7.SetActive(false);
    241.                 _9.SetActive(false);
    242.                 _10.SetActive(false);
    243.                 _11.SetActive(false);
    244.                 _12.SetActive(false);
    245.  
    246.      
    247.  
    248.             }
    249.             if (flag == true && selected == "9")
    250.             {
    251.  
    252.                _9.SetActive(true);
    253.  
    254.                 _1.SetActive(false);
    255.                 _2.SetActive(false);
    256.                 _3.SetActive(false);
    257.                 _4.SetActive(false);
    258.                 _5.SetActive(false);
    259.                 _6.SetActive(false);
    260.                 _7.SetActive(false);
    261.                 _8.SetActive(false);
    262.                 _10.SetActive(false);
    263.                 _11.SetActive(false);
    264.                 _12.SetActive(false);
    265.  
    266.      
    267.  
    268.             }
    269.             if (flag == true && selected == "10")
    270.             {
    271.  
    272.                 _10.SetActive(true);
    273.  
    274.                 _1.SetActive(false);
    275.                 _2.SetActive(false);
    276.                 _3.SetActive(false);
    277.                 _4.SetActive(false);
    278.                 _5.SetActive(false);
    279.                 _6.SetActive(false);
    280.                 _7.SetActive(false);
    281.                 _8.SetActive(false);
    282.                 _9.SetActive(false);
    283.                 _11.SetActive(false);
    284.                 _12.SetActive(false);
    285.  
    286.        
    287.  
    288.             }
    289.             if (flag == true && selected == "11")
    290.             {
    291.  
    292.                 _11.SetActive(true);
    293.  
    294.                 _1.SetActive(false);
    295.                 _2.SetActive(false);
    296.                 _3.SetActive(false);
    297.                 _4.SetActive(false);
    298.                 _5.SetActive(false);
    299.                 _6.SetActive(false);
    300.                 _7.SetActive(false);
    301.                 _8.SetActive(false);
    302.                 _9.SetActive(false);
    303.                 _10.SetActive(false);
    304.                 _12.SetActive(false);
    305.  
    306.  
    307.             }
    308.             if (flag == true && selected == "12")
    309.             {
    310.  
    311.                 _12.SetActive(true);
    312.  
    313.                 _1.SetActive(false);
    314.                 _2.SetActive(false);
    315.                 _3.SetActive(false);
    316.                 _4.SetActive(false);
    317.                 _5.SetActive(false);
    318.                 _6.SetActive(false);
    319.                 _7.SetActive(false);
    320.                 _8.SetActive(false);
    321.                 _9.SetActive(false);
    322.                 _10.SetActive(false);
    323.                 _11.SetActive(false);
    324.  
    325.            
    326.  
    327.             }
    328.  
    329.  
    330.  
    331.      
    332.  
    333.             flag = false;
    334.  
    335.             yield return new WaitUntil(() => flag == true);
    336.        
    337.            
    338.  
    339.  
    340.  
    341.         }
    342.     }
    343.  
    344.     void Shuffle(List<string> lists)
    345.     {
    346.         for (int j = lists.Count - 1; j > 0; j--)
    347.         {
    348.             int rnd = UnityEngine.Random.Range(0, j + 1);
    349.             string temp = lists[j];
    350.             lists[j] = lists[rnd];
    351.             lists[rnd] = temp;
    352.         }
    353.     }
    354. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    This is a fair bit of difficult-to-analyze code because it was written without the use of an array.

    For one, you are adding the listener to the button onClick method at least 12 times, maybe more.

    I suggest instead this approach:

    Put all objects into an array of AudioSources:

    Code (csharp):
    1. public AudioSource[] MyAudioSources;
    In Start():

    - Shuffle that array.

    - Then initialize an integer counter to zero.

    In button response script (when button is pressed):

    - Dereference the array to play the sound that the counter points to and play that sound.

    - Now advance the counter.

    - When the counter reaches the end of the array, shuffle and reset the counter to zero