Search Unity

Game sometimes freeze whenever my music manager switches music

Discussion in 'Scripting' started by PlazmaInteractive, Dec 2, 2016.

  1. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    Hello guys, I've got a problem that I need to fix as soon as possible. My game is nearing completion and this the only major problem I currently got.

    So, my game consist of a Music Manager script. The script basically controls the music in my game. When a music is finished playing, it will choose a different music randomly based on a random number generator and arrays. I got the script online and tweak the code a bit to suit my needs. Also, the Music Manager is placed on a child object of a persistent game object.

    The problem I'm having is sometimes, not every time, my game freezes completely when a music is finished playing and it switches to a new one. If I'm testing my game out in the Editor, it'll freeze completely and the only way to close Unity is to use Task Manager. I built the game (WebGL), thinking that it might be my Unity bugging out but upon testing it, my game freezes too in Firefox and the error that comes up is something like 'script is unresponsive'.

    Here's my Music Manager script:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class MusicManager : MonoBehaviour
    6. {
    7.     [System.Serializable]
    8.     public class MusicClip
    9.     {
    10.         public string clipName;
    11.         public AudioClip audioClip;
    12.     }
    13.  
    14.     public MusicClip[] clips;
    15.  
    16.     private int randomizer;
    17.     private AudioSource audioSource;
    18.     private Text musicText;
    19.  
    20.     void OnEnable()
    21.     {
    22.         // Get audio source in gameobject
    23.         audioSource = GetComponent<AudioSource>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         Scene scene = SceneManager.GetActiveScene();
    29.         int buildIndex = scene.buildIndex;
    30.         // If current scene is not the game intro scene
    31.         if (buildIndex != 1)
    32.             // Finds music text gameobject
    33.             musicText = GameObject.FindWithTag("MusicText").GetComponent<Text>();
    34.  
    35.         if (!audioSource.isPlaying)
    36.         {
    37.             // Gets a random clip and plays it
    38.             audioSource.clip = GetRandomClip();
    39.             audioSource.Play();
    40.         }
    41.  
    42.         if (musicText)
    43.             musicText.text = clips[randomizer].clipName;
    44.     }
    45.  
    46.     private AudioClip GetRandomClip()
    47.     {
    48.         AudioClip clip = null;
    49.  
    50.         // Holder to store current randomized integer
    51.         randomizer = Random.Range(0, clips.Length);
    52.         // Sets current clip
    53.         clip = clips[randomizer].audioClip;
    54.  
    55.         // Make sure clip isn't repeating
    56.         while (clip == audioSource.clip)
    57.             clip = clips[randomizer].audioClip;
    58.  
    59.         return clip;
    60.     }
    61. }
    Is there anything above that might be causing my game to freeze because I have no idea what's causing it. Also, is the script above the best way to do this? Basically, it's a random music playlist so if there's any other scripts out there that is more efficient than this or a fix to my current script, let me know.
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Your culprit is the while loop on line 56. You're not actually regenerating your random number, so if it attempts to find another clip, it's always the same clip because the index is always the same. Any time your code locks up the editor, it's almost certainly an infinite loop.
     
  3. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    Yep it is. I fixed it by adding the 'randomizer = Random.Range(0, clips.Length);' statement inside the while loop and no game freeze occurs afterwards. Thanks!