Okay, not sure if this is the right place to ask or if I did something wrong but ?? aah
So, I have a trigger that I want to play an audio file whenever it is collided with, but I want it to play a different sound each time it is triggered. Not at random, either; I have a sequence of audio files that it must go in. And, once the sequence is completed, I want it to go back to the start. It should be able to repeat infinitely.
What I wrote, I can tell just by looking at it, is definitely not efficient or effective. But with my sparse knowledge of coding, it’s the only way I know how to do it, and it should be working. Plus, the game is not big or complicated, so I don’t have tons and tons of code to manage.
I haven’t seen anyone with the same issue online, so I’ve just had to fend for myself- I have no idea what the proper way to do this is.
My problem at the moment is that it will play the first audio file, but not move to the next one.
My silly way of writing it is 100% the problem, but that’s why I’m asking for help C:
Here’s my code, attached to the player character (which should be the thing to trigger the sound)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SOUNDScript : MonoBehaviour
{
public bool collisionHasHappenedOne = false;
public bool collisionHasHappenedTwo = false;
public bool collisionHasHappenedThree = false;
public bool collisionHasHappenedFour = false;
public bool collisionHasHappenedFive = false;
public bool collisionHasHappenedSix = false;
public bool collisionHasHappenedSeven = false;
public bool collisionHasHappenedEight = false;
public AudioClip firstAudioClip;
public AudioClip secondAudioClip;
public AudioClip thirdAudioClip;
public AudioClip fourthAudioClip;
public AudioClip fifthAudioClip;
public AudioClip sixthAudioClip;
public AudioClip seventhAudioClip;
public AudioClip eighthAudioClip;
AudioSource source;
void Start()
{
source = GetComponent<AudioSource>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collisionHasHappenedOne == false)
{
source.PlayOneShot (firstAudioClip);
collisionHasHappenedOne = true;
}
if (collisionHasHappenedOne == true)
{
source.PlayOneShot (secondAudioClip);
collisionHasHappenedTwo = true;
}
if (collisionHasHappenedTwo == true)
{
source.PlayOneShot(thirdAudioClip);
collisionHasHappenedThree = true;
}
if (collisionHasHappenedThree == true)
{
source.PlayOneShot(fourthAudioClip);
collisionHasHappenedFour = true;
}
if (collisionHasHappenedFour == true)
{
source.PlayOneShot(fifthAudioClip);
collisionHasHappenedFive = true;
}
if (collisionHasHappenedFive == true)
{
source.PlayOneShot(sixthAudioClip);
collisionHasHappenedSix = true;
}
if (collisionHasHappenedFive == true)
{
source.PlayOneShot(seventhAudioClip);
collisionHasHappenedSeven = true;
}
if (collisionHasHappenedSeven == true)
{
source.PlayOneShot(eighthAudioClip);
collisionHasHappenedEight = true;
}
if (collisionHasHappenedEight == true)
{
collisionHasHappenedOne = false;
collisionHasHappenedTwo = false;
collisionHasHappenedThree = false;
collisionHasHappenedFour = false;
collisionHasHappenedFive = false;
collisionHasHappenedSix = false;
collisionHasHappenedSeven = false;
collisionHasHappenedEight = false;
}
}
}
Again, I am very much so aware that this is a silly way to do it, but I don’t know any other way. I don’t have any compiler or runtime errors, it just won’t play the next audio clip in the sequence.
I mean, I’m surprised anything is happening at all since I thought multiple if statements in a method wasn’t even valid?? anyway, any help would be appreciated, thank you :]