how to select and deselct an instantiated objects.


I am making a script. When i press the button a gameobject is instantiated. When i press button again then the another gameobject(clone) is instantiated. What i want to do is make a select and deselect between gameobjects. Lets say that i instantiate 10 objects when game is running. Then i select one gameobject and it turns red. Now i want that if i select another gameobject the the first gameobject will deselect itself and new chosed gameobject will be red.

Basically i dont want to be able to choose more than 1 object. Right now my have made is so that i can select and deselect all the gameobjects when i klick on them. But i just want to able to select one gameobject. And after i select another the first one will get deselected by default.

If this were static objects i just could do a bunch of static boolean fields and make a simple (if check) between them. But all my objects are can be created dynamically at runtime. So i dont know how to make these object interact between each other. All clones are having the same script so how can they know when something other besides them is selected.


you may see this answer: and you only change where it says the tag to a different color. Like this:

renderer.material.color =;

If they share the same script you can share state using static variables. In Javascript `static var someGlobal` and in C# `public static TypeName someGlobal`

Attached to a Cube with a collider

var customColor : Color;


  renderer.material.color = customColor;



 renderer.material.color = Color.white;