- Home /
Same Scene AudioClip Change
Heyo!
Im setting up a game state enum. Im using an array of music files that I want to change between enum states. Ive got the main menu music working. I want it to transition a new song to the game play state. When I trigger to the game play, the song file moves into the audio clip place in the inspector, but nothing plays. My code is display what Im doing. Im obviously going wrong about. What Im doing is reassigning the element of the audioclip array to the appropriate song when the enum changes. If I set the original main menu song in Start(), I get this error:
IndexOutOfRangeException: Array index is out of range. GamePlayWaves.Start () (at Assets/Assets/Scripts/GamePlayWaves.cs:41)
Its looking for the first element in the array. But, if I place it in Awake(), I dont get the error, with the same issue...whats the dilly?
public AudioSource audioListener;
public AudioClip[] gameMusic;
public int gameWaves = 1;
public bool canSpawn = true;
public enum GameModes
{
StartScreen,
GameScreen,
EndScreen
}
public GameModes gameModes = GameModes.StartScreen;
void Awake()
{
mainMenuGUI = GameObject.FindGameObjectsWithTag("MainTitle");
audioListener = gameObject.AddComponent<AudioSource>();
}
void Start()
{
if(gameModes == GameModes.StartScreen)
{
audioListener.clip = gameMusic[0];
audioListener.Play();
}
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.F))
{
gameModes = GameModes.GameScreen;
audioListener.clip = gameMusic[1];
audioListener.Play();
ScreenControl();
}
}
Your answer