randomColor = Random.Range(0,3); Scripting Issue

Hey all, below is my script for spawning enemies. Now what is MEANT to happen is that when the enemies spawn they are given a random color of either blue, red or green. Problem is they all keep coming out blue…

var newEnemy : Enemy = Instantiate (enemy, Vector3(xSpawn, 0, 10), Quaternion.identity); 
 var randomColor = Random.Range(0, 3);

switch (randomColor)
{
	case 0:
		enemy.renderer.material.color = Color.red; //Spawns enemies either red, blue or green
	case 1:
		enemy.renderer.material.color = Color.green; //Spawns enemies either red, blue or green
	case 2: 
	    enemy.renderer.material.color = Color.blue; //Spawns enemies either red, blue or green
}

You’re instantiating a variable called “newEnemy” but then you never refer to it. Instead you’re trying to change the color of the prefab rather than the instantiated enemy. Also, it would be simpler and more maintainable if you used an array instead of a bunch of switch/case code.

var colors = [Color.red, Color.green, Color.blue];
Instantiate (enemy, Vector3(xSpawn, 0, 10), Quaternion.identity).renderer.material.color =
     colors[Random.Range(0, colors.Length)];

(huh. in C# you can’t fall through case statements. did i find JS’s one advantage?)

You’re falling through the case statements. You need to insert a break; command to leave the switch statement entirely or it executes all the way through from whatever case it matches until it hits the end.