No
I have a 2d object called apple which has this script.
I put this object on scene and then i duplicate it to make 2 apples
But now when i click on one apple both apple gets delete.
How to make only delete the apple i click on it
First: Code you showed remove from that script and put it in separate script and then that script attach for example, to Background game object. Tag of first apple should be “FirstApple” and Tag of the second one should be “SecondApple”.
Script on Background should have something like this
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
if (hitInfo.transform.gameObject.tag == "FirstApple")
{
//FindGameObjectsWithTag FirstApple
//destroy that object
} else {
//FindGameObjectsWithTag SecondApple
//destroy that object
}
}
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Destroy(hitInfo.transform.gameObject);
}
}
}