I created multiple same prefab clones like that. and I want to destroy one of them which i select.
First of all i used raycast2d for detecting hit and collied with prefab
if(Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.mousePosition)), Vector2.zero);
if(hit.collider != null && hit.collider.gameObject==gameObject){
health-=PlayerScript.damage;
if(health<=0){
Destroy(hit.collider.gameObject);
}
}
}
this script effect all prefabs health. So i decided create a game object with collider and if this game object collied with prefab , prefab lose health and then destroy. I instantiate the game object like this
GameObject object=Instantiate(selectOb,Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity) as GameObject;
And i check if is there collision between game object and prefab with void OnCollision2DEnter().
Now there is different problem. The game object doesn't collied with other prefab because area sizes of collider
I hope , i could describe my problems. I waiting your advice about selecting specific prefab or my collision problem
prefab
is something in your assets, not in the scene (that's only a formal thing, not related to your problem). Is your raycast script on every instance of your prefab? If so, you should change that to a dedicated gameobject (some sort of controller). Otherwise every clone will execute that script where you would only need one execution. – Gunnar B.