- Home /
How to optimize script that joins objects (problem with Physics.OverlapBox)

I've created a script that joins all objects and makes them destructiable, the script works fine until i decided to join more complex objects, after that unity started lagging during the test. To find the problem i've decided to use profiler which lead me to the script that causes all the problems, i've tried to optimize the code with no results. Does anyone know how could i optimize it?
void JoinPart() { Collider[] hitColliders = Physics.OverlapBox(transform.position, meshbounds / 2f, Quaternion.identity, partMask); foreach (Collider hit in hitColliders) { if (hit.tag == "Ground" && !Joined) //LAGS { gameObject.name = "GroundCheck"; isTouchingGround = true; return; } else if (hit.tag == "Ground" && Joined && !isTouchingGround && TypeOfObject == "Tree") { transform.root.SendMessage("DestroyAll"); }
if (hit.tag == "Part" && hit.GetComponent<JoinCheck>().ObjectName == ObjectName)
{
if (!TriggerList.Contains(hit))
{
TriggerList.Add(hit);
if (!Joined)
{
transform.SetParent(hit.transform);
}
}
}
if (toBeAdded && hit.tag == "Part")
{
//toBeAdded = false;
StartCoroutine("StopAdding");
transform.SetParent(hit.transform);
print(toBeAdded);
}
if (hit.tag == "PartDestroyer")
{
isBroken = true;
}
/*
if((hit.GetComponentInParent<JoinCheck>().ObjectName == ObjectName || hit.GetComponent<JoinCheck>().ObjectName == ObjectName) && Joined && !isBroken)
{
transform.SetParent(hit.transform);
isParentObj = false;
}
*/
}
}
Your answer