Hi,
I’m making a 2D top down RTS style game and would like to know whether the issue I’m having Is a Unity Bug itself or if it’s my code.
The issue happens when I have more than one unit on the screen and try to select either of them the raycast I perform Hits the floor. So, it’s almost as if my unit’s colliders don’t exist. Whats weird is that all my code works when only one unit is active in the scene. This leads me to believe it is my code I just don’t see where the error is.
Any help is greatly appreciated. Thankyou
P.S. Here is the script I believe is housing the issue.
public class UnitSelector : MonoBehaviour
{
RaycastHit2D hit;
RaycastHit2D hit2;
public List<Collider2D> selectedList; // Create the list
public List<GameObject> friendlyUnits;
bool isEnemy;
bool found;
// Use this for initialization
void Start ()
{
selectedList = new List<Collider2D>(); // Make it exsist
friendlyUnits = new List<GameObject>();
}
// Update is called once per frame
void Update ()
{
friendlyUnits.Clear(); // Clear the List so it Updates constantly
foreach(GameObject friendly in GameObject.FindGameObjectsWithTag("BlueSoldier"))// check the world for all friendly units
{
friendlyUnits.Add(friendly); // and Add them to the List
}
if(Input.GetMouseButtonDown(0))// If I Left Click
{
hit = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f);
if(hit.collider.tag == "BlueSoldier")
{
Debug.Log("Hit: " + hit.collider.tag);
isEnemy = false;
if (!Input.GetKey(KeyCode.LeftControl))
{
DeselectUnits();
}
for(int i =0;i<selectedList.Count && !found;i++)
{
if(hit.collider == selectedList*)*
{
found = true;
}
}
if(!found)
{
Debug.Log(“Preparing to Add”);
selectedList.Add(hit.transform.collider2D);
Debug.Log(“Added”);
selectedList[selectedList.Count-1].transform.SendMessage(“SelectUnit”, true);
}
}
else
{
Debug.Log("Hit: "+hit.collider.tag);
//selectedList[selectedList.Count - 1].transform.SendMessage(“SelectUnit”, false);
//selectedList.Clear();
}
}
if(Input.GetMouseButtonDown(1))//If I Right Click
{
hit2 = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f);
if(hit2.collider.tag == “Floor”)
{
Debug.Log(“Floor Clicked”);
selectedList.ForEach(MoveUnit);
}
}
- }*