All sounds play at once on the same gameobject

Hi all,

This is my first Unity project where I code myself, so I am getting into some problem. Any help will be appreciated.
I have a gameobject with 3 different states, and each state should play a different sound. My problem is that when the state changes, all 3 sounds are played at once.

I basically want to change the GameObject’s colour to either red, blue or green. The change needs to happen every 5 seconds, from R->G then G->B then B->R.

The sound enumerates the colour, so my sounds are “red”, “blue” and “green”.

I attached only one AudioSource to my gameObject and didn’t attach any sound file as Audio Clip.
I then created a Js script where I created 3 AudioClip variables. I attach the corresponding sound file to each variable.

I first started with if/else if statements, and got the problem of all sounds playing at once. I found on the forums about using switch rather than else if. I have the same problem with switch. The colour do change but all 3 sounds are played at once.
I attach my 2 scripts. Thanks a lot for your help if you can tell me what I am doing wrong.

IF / ELSE IF VERSION

	static var PickupColour : Color = Color.red;
	var timer : float;
	var red : AudioClip;
	var blue : AudioClip;
	var green : AudioClip;

	function Start () {
		timer = 0;
	}

	function Update () {
		renderer.material.color = PickupColour;
		timer = timer + Time.deltaTime;	
		if (timer > 5.0 && PickupColour == Color.red) {
			timer = 0;
	        audio.PlayOneShot(green);
	        PickupColour = Color.green;
        }
     	else {
     		if (timer > 5.0 && PickupColour == Color.green) {
     			timer = 0;
		        audio.PlayOneShot(blue);
		        PickupColour = Color.blue;
        	}
        	else {
        		if (timer > 5.0 && PickupColour == Color.blue) {
			        timer = 0;
			        audio.PlayOneShot(red);
			        PickupColour = Color.red;
       			 }
       		}
       	}
	}

SWITCH VERSION

	static var PickupColour : Color = Color.red;
	var timer : float;
	var red : AudioClip;
	var blue : AudioClip;
	var green : AudioClip;

	function Start () {
		timer = 0;
	}

	function Update () {
		renderer.material.color = PickupColour;
		timer = timer + Time.deltaTime;	
		if (timer > 10){
			Son();
		}
	}
	
	function Son() {
		switch (PickupColour) {
			case Color.red:
				audio.PlayOneShot(green);
				PickupColour = Color.green;
				timer = 0;
				break;
			case Color.green:
				audio.PlayOneShot(blue);
				PickupColour = Color.blue;
				timer = 0;
				break;		
			case Color.blue:
				audio.PlayOneShot(red);
				PickupColour = Color.red;
				timer = 0;
				break;
		}	
	}

I actually found the problem and the solution. I had several instances of the same GameObject in the scene. So they all verified the condition and played sounds. Still, they played different sounds, which is weird as they were showing the same colour. Maybe there was a frame difference between each instance…

Anyway, I removed the audio files from this GO script. I create a unique GO and added the sounds to it. This 2nd GO checks the colour of the 1st GO and plays the according sound.

I leave this post in the case that another beginner does the same mistake.