I am working on a squad size rts in 2d. I have some troop prefabs that you can spawn with a button but when I click on one of them all of them get selected. I need help getting only one of these instantiate prefab to be selected at on time.
public bool Selected ()
{
Vector2 origin = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x,
Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast (origin, Vector2.zero, 0f); //casts ray to hit somthing
if (hit) {
return true;
}
else
{
return false;
}
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) // get input from the mouse
{
if (select == false) //checks if the unit is selected
{
select = Selected ();
}
else
{
Debug.Log ("error 4");
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
if (move == false)
move = true;
Instantiate (point, target, Quaternion.identity);
select = false;
}
}
I am not sure want I am missing in my code to just select one of the instantiate prefab. please help.