Info on performance

Which of these two codes using less resource?

Better to use the Find or FindChild, because objects are children of Obj1?

void Start()
{
//Example 1

if(value == 0) transform.Find("Obj1").position = new Vector3(mov + transform.position.x,transform.position.y,transform.Find("Obj1").position.z);
else if(value == 1) transform.Find("Obj1").position = new Vector3(-mov + transform.position.x,transform.position.y,transform.Find("Obj1").position.z);
else if(value == 2) transform.Find("Obj1").position = new Vector3(transform.position.x,mov + transform.position.y,transform.Find("Obj1").position.z);
else if(value == 3) transform.Find("Obj1").position = new Vector3(transform.position.x,-mov + transform.position.y,transform.Find("Obj1").position.z);

if(mov != 0)
{
transform.Find("Obj1").Find("Obj2").position = new Vector3((transform.Find("Obj1").position.x + transform.position.x)/2,transform.Find("Obj1").Find("Obj2").position.y,transform.Find("Obj1").Find("Obj2").position.z);
transform.Find("Obj1").Find("Obj2").localScale = new Vector3((transform.Find("Obj1").position.x - transform.position.x)/2,1,1);

transform.Find("Obj1").Find("Obj3").position = new Vector3(transform.Find("Obj1").Find("Obj3").position.x,(transform.Find("Obj1").position.y + transform.position.y)/2,transform.Find("Obj1").Find("Obj3").position.z);
transform.Find("Obj1").Find("Obj3").localScale = new Vector3((transform.Find("Obj1").position.y - transform.position.y)/2,1,1);

transform.Find("Obj1").Find("Obj4").renderer.enabled = true;
}
}

void Start()
{
//Example 2

Transform Obj1Pos = transform.FindChild("Obj1");
Transform Obj2Pos = Obj1Pos.FindChild("Obj2");
Transform Obj3Pos = Obj1Pos.FindChild("Obj3");
Transform Obj4Pos = Obj1Pos.FindChild("Obj4");

if(value == 0) Obj1Pos.position = new Vector3(mov + transform.position.x,transform.position.y,Obj1Pos.position.z);
else if(value == 1) Obj1Pos.position = new Vector3(-mov + transform.position.x,transform.position.y,Obj1Pos.position.z);
else if(value == 2) Obj1Pos.position = new Vector3(transform.position.x,mov + transform.position.y,Obj1Pos.position.z);
else if(value == 3) Obj1Pos.position = new Vector3(transform.position.x,-mov + transform.position.y,Obj1Pos.position.z);

if(mov != 0)
{
Obj2Pos.position = new Vector3((Obj1Pos.position.x + transform.position.x)/2,Obj2Pos.position.y,Obj2Pos.position.z);
Obj2Pos.localScale = new Vector3((Obj1Pos.position.x - transform.position.x)/2,1,1);

Obj3Pos.position = new Vector3(Obj3Pos.position.x,(Obj1Pos.position.y + transform.position.y)/2,Obj3Pos.position.z);
Obj3Pos.localScale = new Vector3((Obj1Pos.position.y - transform.position.y)/2,1,1);

Obj4Pos.renderer.enabled = true;
}
}

Pls Help me

Since you're doing this in the Start function (which runs only once), you're not going to be noticing too big a performance hit, but generally you want to do any kind of Find as little as possible. Instead of finding Obj1, Obj2, Obj3 over and over again like in the first example, Find them once and save a reference as in the second. I think the difference in performance you'd notice between Find and FindChild would be negligible, but should technically be better with FindChild, as it doesn't have to search the whole scene.

Thanks, your answer is perfect !!

1 Like