0
\$\begingroup\$

Could someone help me optimize my code below . I'm not satisfied with this though it gets what i want and execute what i want.

[SerializeField]
GameObject CameraScriptsAir_1,CameraScripstAir_2;

[SerializeField]
GameObject[] CameraScriptsTV;

[SerializeField]
GameObject[] TwoScript;

[SerializeField]
GameObject[] Tests;

[SerializeField]
GameObject FogEffect;

void Update()
{
    if (tzPlayInfo.Instance.BLOCK_GAME_FLOW == false)
    {
        CameraScriptsAir_1 = this.transform.GetChild(0).GetChild(1).GetChild(0).GetChild(2).GetChild(0).gameObject;

        CameraScripstAir_2 = this.transform.GetChild(0).GetChild(1).GetChild(1).GetChild(2).gameObject;

        CameraScriptsTV[0] = this.transform.GetChild(0).GetChild(1).GetChild(5).GetChild(0).GetChild(0).gameObject;
        CameraScriptsTV[1] = this.transform.GetChild(0).GetChild(1).GetChild(6).GetChild(0).GetChild(0).gameObject;
        CameraScriptsTV[2] = this.transform.GetChild(0).GetChild(1).GetChild(7).GetChild(0).GetChild(0).gameObject;

        TwoScript[0] = this.transform.GetChild(0).GetChild(1).GetChild(8).GetChild(0).GetChild(1).gameObject;
        TwoScript[1] = this.transform.GetChild(0).GetChild(1).GetChild(38).GetChild(0).GetChild(0).gameObject;

        //29
        Tests[0] = this.transform.GetChild(0).GetChild(1).GetChild(9).GetChild(0).GetChild(0).gameObject;
        Tests[1] = this.transform.GetChild(0).GetChild(1).GetChild(10).GetChild(0).GetChild(0).gameObject;
        Tests[2] = this.transform.GetChild(0).GetChild(1).GetChild(11).GetChild(0).GetChild(0).gameObject;
        Tests[3] = this.transform.GetChild(0).GetChild(1).GetChild(12).GetChild(0).GetChild(0).gameObject;
        Tests[4] = this.transform.GetChild(0).GetChild(1).GetChild(13).GetChild(0).GetChild(0).gameObject;
        Tests[5] = this.transform.GetChild(0).GetChild(1).GetChild(14).GetChild(0).GetChild(0).gameObject;
        Tests[6] = this.transform.GetChild(0).GetChild(1).GetChild(15).GetChild(0).GetChild(0).gameObject;
        Tests[7] = this.transform.GetChild(0).GetChild(1).GetChild(16).GetChild(0).GetChild(0).gameObject;
        Tests[8] = this.transform.GetChild(0).GetChild(1).GetChild(17).GetChild(0).GetChild(0).gameObject;
        Tests[9] = this.transform.GetChild(0).GetChild(1).GetChild(18).GetChild(0).GetChild(0).gameObject;
        Tests[10] = this.transform.GetChild(0).GetChild(1).GetChild(19).GetChild(0).GetChild(0).gameObject;
        Tests[11] = this.transform.GetChild(0).GetChild(1).GetChild(20).GetChild(0).GetChild(0).gameObject;
        Tests[12] = this.transform.GetChild(0).GetChild(1).GetChild(21).GetChild(0).GetChild(0).gameObject;
        Tests[13] = this.transform.GetChild(0).GetChild(1).GetChild(22).GetChild(0).GetChild(0).gameObject;
        Tests[14] = this.transform.GetChild(0).GetChild(1).GetChild(23).GetChild(0).GetChild(0).gameObject;
        Tests[15] = this.transform.GetChild(0).GetChild(1).GetChild(24).GetChild(0).GetChild(0).gameObject;
        Tests[16] = this.transform.GetChild(0).GetChild(1).GetChild(25).GetChild(0).GetChild(0).gameObject;
        Tests[17] = this.transform.GetChild(0).GetChild(1).GetChild(26).GetChild(0).GetChild(0).gameObject;
        Tests[18] = this.transform.GetChild(0).GetChild(1).GetChild(27).GetChild(0).GetChild(0).gameObject;
        Tests[19] = this.transform.GetChild(0).GetChild(1).GetChild(28).GetChild(0).GetChild(0).gameObject;
        Tests[20] = this.transform.GetChild(0).GetChild(1).GetChild(29).GetChild(0).GetChild(0).gameObject;
        Tests[21] = this.transform.GetChild(0).GetChild(1).GetChild(30).GetChild(0).GetChild(0).gameObject;
        Tests[22] = this.transform.GetChild(0).GetChild(1).GetChild(31).GetChild(0).GetChild(0).gameObject;
        Tests[23] = this.transform.GetChild(0).GetChild(1).GetChild(32).GetChild(0).GetChild(0).gameObject;
        Tests[24] = this.transform.GetChild(0).GetChild(1).GetChild(33).GetChild(0).GetChild(0).gameObject;
        Tests[25] = this.transform.GetChild(0).GetChild(1).GetChild(34).GetChild(0).GetChild(0).gameObject;
        Tests[26] = this.transform.GetChild(0).GetChild(1).GetChild(35).GetChild(0).GetChild(0).gameObject;
        Tests[27] = this.transform.GetChild(0).GetChild(1).GetChild(36).GetChild(0).GetChild(0).gameObject;
        Tests[28] = this.transform.GetChild(0).GetChild(1).GetChild(37).GetChild(0).GetChild(0).gameObject;

        //fog
        FogEffect = this.gameObject.transform.GetChild(0).GetChild(2).GetChild(0).GetChild(2).gameObject;
    }
}
\$\endgroup\$
12
  • 1
    \$\begingroup\$ Do you know what for loops are? \$\endgroup\$
    – Bálint
    Jan 15, 2018 at 8:30
  • 3
    \$\begingroup\$ Then maybe start with learning programming. Don't jump into unity without that \$\endgroup\$
    – Bálint
    Jan 15, 2018 at 8:34
  • 1
    \$\begingroup\$ @NoobProgrammer I've answered your question to give you an example but your question is not very clear as to what you are trying to achieve, provide more information so I can help you better. As mentioned you should probably learn some programming first before you jump into unity! \$\endgroup\$
    – Valamorde
    Jan 15, 2018 at 9:36
  • 3
    \$\begingroup\$ By the way, we also have codereview.stackexchange.com - a stackexchange site specifically for getting constructive feedback for code you wrote. \$\endgroup\$
    – Philipp
    Jan 15, 2018 at 10:13
  • 1
    \$\begingroup\$ @NoobProgrammer You have accepted an answer, but you haven't finished clarifying what you're doing for the answer to truly be THE answer. Why are you traversing an object hierarchy in that manner? What is the hierarchy of in the first place? In order for a question to have value to other people, you have to clearly state the problem you are trying to solve, not just an obscure code snippit. \$\endgroup\$
    – Stephan
    Jan 15, 2018 at 16:38

1 Answer 1

2
\$\begingroup\$

You could (and should) use a for loop, for example this:

    Tests[0] = this.transform.GetChild(0).GetChild(1).GetChild(9).GetChild(0).GetChild(0).gameObject;
    Tests[1] = this.transform.GetChild(0).GetChild(1).GetChild(10).GetChild(0).GetChild(0).gameObject;
    Tests[2] = this.transform.GetChild(0).GetChild(1).GetChild(11).GetChild(0).GetChild(0).gameObject;
    Tests[3] = this.transform.GetChild(0).GetChild(1).GetChild(12).GetChild(0).GetChild(0).gameObject;
    Tests[4] = this.transform.GetChild(0).GetChild(1).GetChild(13).GetChild(0).GetChild(0).gameObject;
    Tests[5] = this.transform.GetChild(0).GetChild(1).GetChild(14).GetChild(0).GetChild(0).gameObject;
    Tests[6] = this.transform.GetChild(0).GetChild(1).GetChild(15).GetChild(0).GetChild(0).gameObject;
    Tests[7] = this.transform.GetChild(0).GetChild(1).GetChild(16).GetChild(0).GetChild(0).gameObject;
    Tests[8] = this.transform.GetChild(0).GetChild(1).GetChild(17).GetChild(0).GetChild(0).gameObject;
    Tests[9] = this.transform.GetChild(0).GetChild(1).GetChild(18).GetChild(0).GetChild(0).gameObject;
    Tests[10] = this.transform.GetChild(0).GetChild(1).GetChild(19).GetChild(0).GetChild(0).gameObject;
    Tests[11] = this.transform.GetChild(0).GetChild(1).GetChild(20).GetChild(0).GetChild(0).gameObject;
    Tests[12] = this.transform.GetChild(0).GetChild(1).GetChild(21).GetChild(0).GetChild(0).gameObject;
    Tests[13] = this.transform.GetChild(0).GetChild(1).GetChild(22).GetChild(0).GetChild(0).gameObject;
    Tests[14] = this.transform.GetChild(0).GetChild(1).GetChild(23).GetChild(0).GetChild(0).gameObject;
    Tests[15] = this.transform.GetChild(0).GetChild(1).GetChild(24).GetChild(0).GetChild(0).gameObject;
    Tests[16] = this.transform.GetChild(0).GetChild(1).GetChild(25).GetChild(0).GetChild(0).gameObject;
    Tests[17] = this.transform.GetChild(0).GetChild(1).GetChild(26).GetChild(0).GetChild(0).gameObject;
    Tests[18] = this.transform.GetChild(0).GetChild(1).GetChild(27).GetChild(0).GetChild(0).gameObject;
    Tests[19] = this.transform.GetChild(0).GetChild(1).GetChild(28).GetChild(0).GetChild(0).gameObject;
    Tests[20] = this.transform.GetChild(0).GetChild(1).GetChild(29).GetChild(0).GetChild(0).gameObject;
    Tests[21] = this.transform.GetChild(0).GetChild(1).GetChild(30).GetChild(0).GetChild(0).gameObject;
    Tests[22] = this.transform.GetChild(0).GetChild(1).GetChild(31).GetChild(0).GetChild(0).gameObject;
    Tests[23] = this.transform.GetChild(0).GetChild(1).GetChild(32).GetChild(0).GetChild(0).gameObject;
    Tests[24] = this.transform.GetChild(0).GetChild(1).GetChild(33).GetChild(0).GetChild(0).gameObject;
    Tests[25] = this.transform.GetChild(0).GetChild(1).GetChild(34).GetChild(0).GetChild(0).gameObject;
    Tests[26] = this.transform.GetChild(0).GetChild(1).GetChild(35).GetChild(0).GetChild(0).gameObject;
    Tests[27] = this.transform.GetChild(0).GetChild(1).GetChild(36).GetChild(0).GetChild(0).gameObject;
    Tests[28] = this.transform.GetChild(0).GetChild(1).GetChild(37).GetChild(0).GetChild(0).gameObject;

could be simplified to this:

for(int i=0; i<29; i++)
{
    Tests[i]= this. transform.GetChild(0).GetChild(1).GetChild(i+9).GetChild(0).GetChild(0).gameObject; //what are you trying to get here???? this still is not right!
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .