Selecting unique clones - Beginner Question

Hi all,

I have created a small script that spawns 6 different objects on start. I want the object that I clicked on to change color. I have assigned unique names to each object. However, when I use onMouseDown, all objects change color. How do I tell unity that I only want that specific object to change? Unfortunately, I could not find a good answer to this on the forums.

var isSelected : boolean;
var playerPrefab : Rigidbody;
var numberOfPlayers : int = 6;

function Start () {
      var x : int;
      for(x=0;x<numberOfPlayers; x++){
      var player = Instantiate(playerPrefab, Vector3(x*2.0,0.5,0.0), transform.rotation);
      player.name = "player" + x;
}

function Update(){
      if(isSelected){
    	    renderer.material.color = Color.red;
      }
}

function OnMouseDown () {
	   
      isSelected = true;

}

From what I’m seeing, you’re changing the color of the object that is spawning your 6 objects? Why don’t you try creating a separate script for your objects that you are spawning to only have the OnMouseDown function while having only 1 script to spawn your objects?

So every player should have a script? Isn’t that bad practice?

I belive you could end the startfunction with a code that break the prefab connection once the object got created, or if it exist. Doing so would make each created object unic, so only the one object you click would change colour. Check this out:

http://answers.unity3d.com/questions/11138/breaking-a-prefab-connection.html?page=2&pageSize=5&sort=oldest

Another tread you might find helpful
http://forum.unity3d.com/threads/19450-Detecting-mouse-click-on-object