Im making simple kill ants game to practise some C#. Im spawning objects(ant) randomly and destroy them when i click. But now when i leave more than 1 ant in screen click will destroy all.
I just want that 1 gameobject(ant) will get destroyed.
click and destroy script is attached to object(ant) what i am spawnin with gamecontroller script
void FixedUpdate ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
Destroy (gameObject);
}
}
}
}
spawn script
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public Camera cam;
public GameObject ant;
private float maxWidth;
private float maxHeight;
void Start ()
{
if (cam == null)
{
cam = Camera.main;
}
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 target_xy = cam.ScreenToWorldPoint (upperCorner);
maxWidth = target_xy.x;
maxHeight = target_xy.y;
StartCoroutine (Spawn ());
}
IEnumerator Spawn ()
{
yield return new WaitForSeconds (2.0f);
while (true)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-maxWidth, maxWidth), Random.Range (-maxHeight, maxHeight), 0.0f);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (ant, spawnPosition, spawnRotation);
yield return new WaitForSeconds (Random.Range(1.0f, 2.0f));
}
}
}
need some help