Search Unity

Resolved Playing a random song and removing it from the list

Discussion in 'Scripting' started by nilsquestier, Feb 13, 2024.

  1. nilsquestier

    nilsquestier

    Joined:
    Sep 18, 2020
    Posts:
    3
    Hi everyone I'm new to the forum so let me know if I did something wrong while making this post. With that out of the way here is my question.

    I'm trying to make a script that selects a random song to play and then removes it from the list so that when a next random song is chosen it can't play the same one again until all songs have been played. I all songs have been played the list will be reset to its original state with all it containing all songs again.

    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class gamemusic : MonoBehaviour
    6. {
    7.     public List<AudioSource> music;
    8.     public List<AudioSource> OGmusic;
    9.     private int rand;
    10.  
    11.     void Start()
    12.     {
    13.         int rand = Random.Range(0, music.Count);
    14.         music[rand].Play();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (music[rand].isPlaying == false)
    20.         {
    21.             MusicCheck();
    22.         }
    23.     }
    24.  
    25.     void MusicCheck()
    26.     {
    27.         music.RemoveAt(rand);
    28.         int rand = Random.Range(0, music.Count);
    29.         music[rand].Play();
    30.  
    31.         if(music.Count == 0)
    32.         {
    33.             music = OGmusic;
    34.         }
    35.     }
    36. }
    The problem is that now unity is saying that I'm using the variabel "rand" before declaring it. I'm sure there is a simple way to fix this but I've been fiddling with the code for a while now and haven't found a solution yet.
     
    Last edited: Feb 13, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
  3. nilsquestier

    nilsquestier

    Joined:
    Sep 18, 2020
    Posts:
    3
    Am I not already declaring it in "void Start()"? Shouldn't that be enough? Even is I give the int a number at the beginning of my code when I declare my variables I still get the same error. Btw I updated the post and added code tags thank you for mentioning that.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Time to go review some C#.

    The line beginning with
    int rand
    is what declares the variable.

    You're using it BEFORE that line, which is exactly what the error says, and it is clear you read it because you paraphrased it.

     
  5. nilsquestier

    nilsquestier

    Joined:
    Sep 18, 2020
    Posts:
    3
    Ok thank you I finally see what I did wrong. I didn't understand the difference between "int rand =" and "rand =" but now I do. I just removed the "int" and now it works. Thanks a lot.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Oh yeah, you actually had TWO variables going, the class instance variable on line 9 and the local variable that was causing the error. I didn't even notice that.

    You may wish to use a better name than
    rand
    , such as
    chosenSongIndex
    or something with at least some semantic meaning.
     
    nilsquestier likes this.