Question by
KoenVNL · Mar 17, 2019 at 08:48 AM ·
destroy objectrange
How do I destroy multiple game objects in a specific range of my player?
I made this, but I think it can be coded much easier.
public float radius = 5f;
public void DestroyWall()
{
GameObject Wall = GameObject.Find("Wall");
GameObject Wall1 = GameObject.Find("Wall (1)");
GameObject Wall2 = GameObject.Find("Wall (2)");
GameObject Wall3 = GameObject.Find("Wall (3)");
if (Wall != null)
{
float dist = Vector3.Distance(transform.position, Wall.transform.position);
if (dist <= radius)
{
Destroy(Wall.gameObject);
}
}
if (Wall1 != null)
{
float dist1 = Vector3.Distance(transform.position, Wall1.transform.position);
if (dist1 <= radius)
{
Destroy(Wall1.gameObject);
}
}
if (Wall2 != null)
{
float dist2 = Vector3.Distance(transform.position, Wall2.transform.position);
if (dist2 <= radius)
{
Destroy(Wall2.gameObject);
}
}
if (Wall3 != null)
{
float dist3 = Vector3.Distance(transform.position, Wall3.transform.position);
if (dist3 <= radius)
{
Destroy(Wall3.gameObject);
}
}
Could you help me out? Thank you in advance!
Comment
Your answer
