Deslecting previous selection

Ok, I’ve got a code attached to a cube prefab, and 3 instances of this prefab in a scene. So far, the code highlights the material of each of the cubes when the mouse cursor is over them. Also, when I click one of the cubes it turns green. Jolly impressive I reckon :stuck_out_tongue: Here’s the code:

var materials: UnityEngine.Material[];
static var allySelected = false;
private var clicked = false;
function Update () {
if (allySelected) {print("Selected");}
if (clicked){allySelected = true;}
}
function OnMouseDown ()
{clicked = true;}
function OnMouseOver () {
if (!clicked){
renderer.sharedMaterial = materials[1];}
else {renderer.sharedMaterial = materials[2];}
}
function OnMouseExit ()
{if (!clicked) {renderer.sharedMaterial = materials[0];}}

Two of the variables are static or private which is just because I was trying to figure out how to solve my next problem. I can’t figure out how I’d script when one cube goes green when I click it, any of the other green cubes go red. Think troop selection in RTS games, individually selecting one automatically deselects any previously selected troop. Is it something to do with static variables, or referencing instances of the prefab? Cheers for your help :smile:

You need a controller to do this, You can’t really do it on a multiple objects. Just have a game controller “listen” for a mouse click, do a raycast from the screen to the world, if that object is something that can be selected, then if you have an old object selected, unselect it. Select the new object.

This process can be done with a List object so you can select multiple objects. Just go through the old list, unselect everything, make your changes, then reselect everything.

By selecting, you would want to maybe change the color from a gray 0.8, 0.8, 0.8 to 1.0, 1.0, 1.0 and maybe also place a projector in the screen to put a circle under the object or something like that. When it is done, simply destroy the projector and return teh color back to 0.8, 0.8, 0.8.

Rightio cheers. I’d better research what controllers and raycasting are then! :smile:

I’m not really sure how raycasting would work in this instance, could you explain a bit further?

I may have explained what I needed pretty badly. In a turn based strategy game you can select a troop and it highlights the unit in some way to visibly show that it is selected (the projected circle is a fantastic idea btw and I’ll definitely look into that).

However, when a different troop is selected, the previous one goes back to its original colour and I can’t figure this out. The reason is, I don’t know how scripts attached to game objects can affect or altar scripts attached to other game objects. Presumably you can reference them in some way, but how could you reference prefabs?

In my scene currently, I have 3 red cubes. When I put my cursor over any of them they turn a lighter shade of red. When the mouse exits them, it reverts back to the original shade of red. When I click a cube it goes green. So, I can click all three cubes one after the other to turn them all permanently green. However, I want to make it so that I select the first cube, it goes green, then select the second cube (making it green) which makes the first cube go back to red.

Sorry for the long winded explanation, hope anyone can help. Thanks a lot.