Okay so I have two cubes and I want it so when I click the first cube it is selected and when i click the second cube it deselects the first cube and selects the second cube. How would I go about doing this? Here is the first script I tried:
#pragma strict
var selected = false;
var clickCount = 0;
function Start () {
}
function OnMouseOver() {
if(Input.GetMouseButtonDown(0)){
clickCount = clickCount + 1;
}
if(Input.GetMouseButtonDown(0))
{
if(clickCount%2==1)
{
selected = true;
Debug.Log("Grid Peice Selected");
}
}
if(Input.GetMouseButtonDown(0))
{
if(clickCount%2==0)
{
selected = false;
Debug.Log("Grid Peice Unselected");
}
}
}
This is the second script I tried this time using raycast:
#pragma strict
var selected = false;
function OnMouseDown()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, 100))
{
Debug.Log("Selected");
}
}else{
Debug.Log("Unselected");
}
}