4 level deep of For loops for counting objects

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I'm just going to ask about this 4 Level Deep For Loop of Mine though i put it inside of the StartCoroutine();



Here is my code : please don't judge me yet



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;

//get the Camera with Bloom Script
for (int i = 0; i < maxCameraScriptTV; i++)

CameraScriptsTV[i] = this.transform.GetChild(0).GetChild(1).GetChild(i + 5).GetChild(0).GetChild(0).gameObject;


//get the Camera With DepthOfField Script and Bloom Script
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;

//9 - 11
//get only the depthoffield.cs
for (int j = 0; j < maxTests1; j++)

Tests1[j] = this.transform.GetChild(0).GetChild(1).GetChild(j + 9).GetChild(0).GetChild(0).gameObject;


//get all the Test with the camera Bloom script
for (int k = 0; k < maxTests2; k++)

Tests2[k] = this.transform.GetChild(0).GetChild(1).GetChild(k + 12).GetChild(0).GetChild(0).gameObject;


//get the fog GameObject
FogEffect = this.gameObject.transform.GetChild(0).GetChild(2).GetChild(0).GetChild(2).gameObject;

if (!foundAllComponents)

StartCoroutine(CountObject());
else

StopCoroutine(CountObject());




if (foundAllComponents)

if (OptionPopup.isCheckedCamera == true)

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = false;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScripstAir_2.GetComponent<Bloom>().enabled = false;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = false;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = false;
two.GetComponent<Bloom>().enabled = false;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = false;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = false;


else

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = true;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScripstAir_2.GetComponent<Bloom>().enabled = true;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = true;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = true;
two.GetComponent<Bloom>().enabled = true;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = true;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = true;



if (OptionPopup.isCheckedFog == true)

FogEffect.SetActive(false);

else

FogEffect.SetActive(true);

else

Debug.Log("Wait for the component to be found!!");




IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



On the last part of my code



IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



This one is just for counting of my object so that i can put it inside an if else statement . I could not compare an Array of GameObject to null if i'm not going to get its Length right ? So is my way of doing it is safe or not? and did i use my coroutine right cause i'm a bit conscious of it. I'll accept any discrimination on my code.







share|improve this question





















  • Just about your last snippet: are you just testing CameraScriptsTV.All(NotNull) && TwoScripts.All(NotNull) && ...? You can write it like that (where static bool NotNull(object obj) => obj != null;). Also you're returning an IEnumerator but yeld return generates an IEnumerable<T> and...there isn't any enumeration there. What for?
    – Adriano Repetti
    Jan 16 at 10:42

















up vote
3
down vote

favorite












I'm just going to ask about this 4 Level Deep For Loop of Mine though i put it inside of the StartCoroutine();



Here is my code : please don't judge me yet



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;

//get the Camera with Bloom Script
for (int i = 0; i < maxCameraScriptTV; i++)

CameraScriptsTV[i] = this.transform.GetChild(0).GetChild(1).GetChild(i + 5).GetChild(0).GetChild(0).gameObject;


//get the Camera With DepthOfField Script and Bloom Script
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;

//9 - 11
//get only the depthoffield.cs
for (int j = 0; j < maxTests1; j++)

Tests1[j] = this.transform.GetChild(0).GetChild(1).GetChild(j + 9).GetChild(0).GetChild(0).gameObject;


//get all the Test with the camera Bloom script
for (int k = 0; k < maxTests2; k++)

Tests2[k] = this.transform.GetChild(0).GetChild(1).GetChild(k + 12).GetChild(0).GetChild(0).gameObject;


//get the fog GameObject
FogEffect = this.gameObject.transform.GetChild(0).GetChild(2).GetChild(0).GetChild(2).gameObject;

if (!foundAllComponents)

StartCoroutine(CountObject());
else

StopCoroutine(CountObject());




if (foundAllComponents)

if (OptionPopup.isCheckedCamera == true)

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = false;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScripstAir_2.GetComponent<Bloom>().enabled = false;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = false;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = false;
two.GetComponent<Bloom>().enabled = false;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = false;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = false;


else

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = true;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScripstAir_2.GetComponent<Bloom>().enabled = true;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = true;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = true;
two.GetComponent<Bloom>().enabled = true;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = true;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = true;



if (OptionPopup.isCheckedFog == true)

FogEffect.SetActive(false);

else

FogEffect.SetActive(true);

else

Debug.Log("Wait for the component to be found!!");




IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



On the last part of my code



IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



This one is just for counting of my object so that i can put it inside an if else statement . I could not compare an Array of GameObject to null if i'm not going to get its Length right ? So is my way of doing it is safe or not? and did i use my coroutine right cause i'm a bit conscious of it. I'll accept any discrimination on my code.







share|improve this question





















  • Just about your last snippet: are you just testing CameraScriptsTV.All(NotNull) && TwoScripts.All(NotNull) && ...? You can write it like that (where static bool NotNull(object obj) => obj != null;). Also you're returning an IEnumerator but yeld return generates an IEnumerable<T> and...there isn't any enumeration there. What for?
    – Adriano Repetti
    Jan 16 at 10:42













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm just going to ask about this 4 Level Deep For Loop of Mine though i put it inside of the StartCoroutine();



Here is my code : please don't judge me yet



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;

//get the Camera with Bloom Script
for (int i = 0; i < maxCameraScriptTV; i++)

CameraScriptsTV[i] = this.transform.GetChild(0).GetChild(1).GetChild(i + 5).GetChild(0).GetChild(0).gameObject;


//get the Camera With DepthOfField Script and Bloom Script
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;

//9 - 11
//get only the depthoffield.cs
for (int j = 0; j < maxTests1; j++)

Tests1[j] = this.transform.GetChild(0).GetChild(1).GetChild(j + 9).GetChild(0).GetChild(0).gameObject;


//get all the Test with the camera Bloom script
for (int k = 0; k < maxTests2; k++)

Tests2[k] = this.transform.GetChild(0).GetChild(1).GetChild(k + 12).GetChild(0).GetChild(0).gameObject;


//get the fog GameObject
FogEffect = this.gameObject.transform.GetChild(0).GetChild(2).GetChild(0).GetChild(2).gameObject;

if (!foundAllComponents)

StartCoroutine(CountObject());
else

StopCoroutine(CountObject());




if (foundAllComponents)

if (OptionPopup.isCheckedCamera == true)

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = false;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScripstAir_2.GetComponent<Bloom>().enabled = false;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = false;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = false;
two.GetComponent<Bloom>().enabled = false;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = false;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = false;


else

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = true;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScripstAir_2.GetComponent<Bloom>().enabled = true;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = true;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = true;
two.GetComponent<Bloom>().enabled = true;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = true;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = true;



if (OptionPopup.isCheckedFog == true)

FogEffect.SetActive(false);

else

FogEffect.SetActive(true);

else

Debug.Log("Wait for the component to be found!!");




IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



On the last part of my code



IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



This one is just for counting of my object so that i can put it inside an if else statement . I could not compare an Array of GameObject to null if i'm not going to get its Length right ? So is my way of doing it is safe or not? and did i use my coroutine right cause i'm a bit conscious of it. I'll accept any discrimination on my code.







share|improve this question













I'm just going to ask about this 4 Level Deep For Loop of Mine though i put it inside of the StartCoroutine();



Here is my code : please don't judge me yet



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;

//get the Camera with Bloom Script
for (int i = 0; i < maxCameraScriptTV; i++)

CameraScriptsTV[i] = this.transform.GetChild(0).GetChild(1).GetChild(i + 5).GetChild(0).GetChild(0).gameObject;


//get the Camera With DepthOfField Script and Bloom Script
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;

//9 - 11
//get only the depthoffield.cs
for (int j = 0; j < maxTests1; j++)

Tests1[j] = this.transform.GetChild(0).GetChild(1).GetChild(j + 9).GetChild(0).GetChild(0).gameObject;


//get all the Test with the camera Bloom script
for (int k = 0; k < maxTests2; k++)

Tests2[k] = this.transform.GetChild(0).GetChild(1).GetChild(k + 12).GetChild(0).GetChild(0).gameObject;


//get the fog GameObject
FogEffect = this.gameObject.transform.GetChild(0).GetChild(2).GetChild(0).GetChild(2).gameObject;

if (!foundAllComponents)

StartCoroutine(CountObject());
else

StopCoroutine(CountObject());




if (foundAllComponents)

if (OptionPopup.isCheckedCamera == true)

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = false;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = false;
CameraScripstAir_2.GetComponent<Bloom>().enabled = false;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = false;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = false;
two.GetComponent<Bloom>().enabled = false;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = false;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = false;


else

CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScriptsAir_1.GetComponent<Bloom>().enabled = true;
CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = true;
CameraScripstAir_2.GetComponent<Bloom>().enabled = true;

foreach (GameObject camScriptTV in CameraScriptsTV)

camScriptTV.GetComponent<Bloom>().enabled = true;


foreach (GameObject two in TwoScript)

two.GetComponent<DepthOfFieldDeprecated>().enabled = true;
two.GetComponent<Bloom>().enabled = true;


foreach (GameObject test in Tests1)

test.GetComponent<DepthOfFieldDeprecated>().enabled = true;


foreach (GameObject test in Tests2)

test.GetComponent<Bloom>().enabled = true;



if (OptionPopup.isCheckedFog == true)

FogEffect.SetActive(false);

else

FogEffect.SetActive(true);

else

Debug.Log("Wait for the component to be found!!");




IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



On the last part of my code



IEnumerator CountObject()

//count the object inside the camera objects
for (int i = 0; i < CameraScriptsTV.Length; i++)

//count the object inside the twoscript objects
for (int j = 0; j < TwoScript.Length; j++)

//count for tests1 objects
for (int k = 0; k < Tests1.Length; k++)

//count for tests2 objects
for (int l = 0; l < Tests2.Length; l++)

//check all the objects if they're not null
if (CameraScriptsAir_1 && CameraScripstAir_2 && FogEffect != null && CameraScriptsTV[i] != null && TwoScript[j] != null && Tests1[k] != null && Tests2[l] != null)

Debug.Log("GameObject is not null");
foundAllComponents = true;

else

Debug.Log("Lets wait for the game object to be attached");





yield return null;



This one is just for counting of my object so that i can put it inside an if else statement . I could not compare an Array of GameObject to null if i'm not going to get its Length right ? So is my way of doing it is safe or not? and did i use my coroutine right cause i'm a bit conscious of it. I'll accept any discrimination on my code.









share|improve this question












share|improve this question




share|improve this question








edited Jan 16 at 7:34









t3chb0t

32.1k54195




32.1k54195









asked Jan 16 at 7:31









NoobProgrammer

1182




1182











  • Just about your last snippet: are you just testing CameraScriptsTV.All(NotNull) && TwoScripts.All(NotNull) && ...? You can write it like that (where static bool NotNull(object obj) => obj != null;). Also you're returning an IEnumerator but yeld return generates an IEnumerable<T> and...there isn't any enumeration there. What for?
    – Adriano Repetti
    Jan 16 at 10:42

















  • Just about your last snippet: are you just testing CameraScriptsTV.All(NotNull) && TwoScripts.All(NotNull) && ...? You can write it like that (where static bool NotNull(object obj) => obj != null;). Also you're returning an IEnumerator but yeld return generates an IEnumerable<T> and...there isn't any enumeration there. What for?
    – Adriano Repetti
    Jan 16 at 10:42
















Just about your last snippet: are you just testing CameraScriptsTV.All(NotNull) && TwoScripts.All(NotNull) && ...? You can write it like that (where static bool NotNull(object obj) => obj != null;). Also you're returning an IEnumerator but yeld return generates an IEnumerable<T> and...there isn't any enumeration there. What for?
– Adriano Repetti
Jan 16 at 10:42





Just about your last snippet: are you just testing CameraScriptsTV.All(NotNull) && TwoScripts.All(NotNull) && ...? You can write it like that (where static bool NotNull(object obj) => obj != null;). Also you're returning an IEnumerator but yeld return generates an IEnumerable<T> and...there isn't any enumeration there. What for?
– Adriano Repetti
Jan 16 at 10:42











3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










Your code is going to log an awful lot of "GameObject is not null" items, for very few non-null game objects.



Basically, you've made an O(n * m * p * q) algorithm, where all you needed was (at most) an O(n + m + p + q) algorithm. This would achieve the same effect - beyond the fact that you'll get fewer logs of the same non-null combination over and over again.



This code assumes that the library requires an enumerator.



IEnumerator CountObject()

if (CameraScriptsAir_1 &&
CameraScripstAir_2 &&
FogEffect != null &&
CameraScriptsTV.Any(e => e != null) &&
TwoScript.Any(e => e != null) &&
Tests1.Any(e => e != null) &&
Tests2.Any(e => e != null))

foundAllComponents = true;
Debug.Log("GameObject is not null");

else

Debug.Log("Lets wait for the game object to be attached");

yield break;






share|improve this answer




























    up vote
    3
    down vote













    Some very general comments:



    I would suggest that the code needs to be more readable, because for someone who is not familiar with it - it’s difficult to discern what is going on.



    • You can immediately improve the code just by giving better names for variables/methods. What is tzPlayInfo? What is its instance?

    • Why are we getting the children of children of children: this.transform.GetChild(0).GetChild(1).GetChild(1).GetChild(2).gameObject; - surely there’s a better way? What is gameObject - can you think of a more descriptive name. Also this looks like it's violating the law of demeter (please google it if you're not familiar with it.)

    • There are way too many if statements for it to be understandable - so perhaps rethink the algorithm.

    • I recommend breaking up the code into many separate methods - i.e. a good rule of thumb, as an exercise, would be to make each method no more than 10 lines long. This will force you to break things up. Right now it's a wall of code/text.

    • Typically folks would return an Enumerable<T> rather than an Enumerator – is there any reason why an enumerator is returned?
      - Avoid deep nesting. It could take a very long time as N increases.





    share|improve this answer






























      up vote
      1
      down vote













      • Why do you need to return an enumerator? It's only useful if you yield return items to make a list. Here you just return null which is the same as returning true or false.


      • Why do you need the nested loop? What are you trying to do? Right now when each of the lists first item is not null, you already set the variable allFound to true but you keep on looping. So if the next item is null, you still set it to allFound. Seems strange.


      • Naming. GetChild().GetChild(). Much easier to read as GetTest(). It's also known as a "train wreck", all the nested GetChilds. One solution is to have a method in the top class return all the data you need.






      share|improve this answer























        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "196"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );








         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185196%2f4-level-deep-of-for-loops-for-counting-objects%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        Your code is going to log an awful lot of "GameObject is not null" items, for very few non-null game objects.



        Basically, you've made an O(n * m * p * q) algorithm, where all you needed was (at most) an O(n + m + p + q) algorithm. This would achieve the same effect - beyond the fact that you'll get fewer logs of the same non-null combination over and over again.



        This code assumes that the library requires an enumerator.



        IEnumerator CountObject()

        if (CameraScriptsAir_1 &&
        CameraScripstAir_2 &&
        FogEffect != null &&
        CameraScriptsTV.Any(e => e != null) &&
        TwoScript.Any(e => e != null) &&
        Tests1.Any(e => e != null) &&
        Tests2.Any(e => e != null))

        foundAllComponents = true;
        Debug.Log("GameObject is not null");

        else

        Debug.Log("Lets wait for the game object to be attached");

        yield break;






        share|improve this answer

























          up vote
          1
          down vote



          accepted










          Your code is going to log an awful lot of "GameObject is not null" items, for very few non-null game objects.



          Basically, you've made an O(n * m * p * q) algorithm, where all you needed was (at most) an O(n + m + p + q) algorithm. This would achieve the same effect - beyond the fact that you'll get fewer logs of the same non-null combination over and over again.



          This code assumes that the library requires an enumerator.



          IEnumerator CountObject()

          if (CameraScriptsAir_1 &&
          CameraScripstAir_2 &&
          FogEffect != null &&
          CameraScriptsTV.Any(e => e != null) &&
          TwoScript.Any(e => e != null) &&
          Tests1.Any(e => e != null) &&
          Tests2.Any(e => e != null))

          foundAllComponents = true;
          Debug.Log("GameObject is not null");

          else

          Debug.Log("Lets wait for the game object to be attached");

          yield break;






          share|improve this answer























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            Your code is going to log an awful lot of "GameObject is not null" items, for very few non-null game objects.



            Basically, you've made an O(n * m * p * q) algorithm, where all you needed was (at most) an O(n + m + p + q) algorithm. This would achieve the same effect - beyond the fact that you'll get fewer logs of the same non-null combination over and over again.



            This code assumes that the library requires an enumerator.



            IEnumerator CountObject()

            if (CameraScriptsAir_1 &&
            CameraScripstAir_2 &&
            FogEffect != null &&
            CameraScriptsTV.Any(e => e != null) &&
            TwoScript.Any(e => e != null) &&
            Tests1.Any(e => e != null) &&
            Tests2.Any(e => e != null))

            foundAllComponents = true;
            Debug.Log("GameObject is not null");

            else

            Debug.Log("Lets wait for the game object to be attached");

            yield break;






            share|improve this answer













            Your code is going to log an awful lot of "GameObject is not null" items, for very few non-null game objects.



            Basically, you've made an O(n * m * p * q) algorithm, where all you needed was (at most) an O(n + m + p + q) algorithm. This would achieve the same effect - beyond the fact that you'll get fewer logs of the same non-null combination over and over again.



            This code assumes that the library requires an enumerator.



            IEnumerator CountObject()

            if (CameraScriptsAir_1 &&
            CameraScripstAir_2 &&
            FogEffect != null &&
            CameraScriptsTV.Any(e => e != null) &&
            TwoScript.Any(e => e != null) &&
            Tests1.Any(e => e != null) &&
            Tests2.Any(e => e != null))

            foundAllComponents = true;
            Debug.Log("GameObject is not null");

            else

            Debug.Log("Lets wait for the game object to be attached");

            yield break;







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jan 16 at 20:29









            Adam Brown

            2993




            2993






















                up vote
                3
                down vote













                Some very general comments:



                I would suggest that the code needs to be more readable, because for someone who is not familiar with it - it’s difficult to discern what is going on.



                • You can immediately improve the code just by giving better names for variables/methods. What is tzPlayInfo? What is its instance?

                • Why are we getting the children of children of children: this.transform.GetChild(0).GetChild(1).GetChild(1).GetChild(2).gameObject; - surely there’s a better way? What is gameObject - can you think of a more descriptive name. Also this looks like it's violating the law of demeter (please google it if you're not familiar with it.)

                • There are way too many if statements for it to be understandable - so perhaps rethink the algorithm.

                • I recommend breaking up the code into many separate methods - i.e. a good rule of thumb, as an exercise, would be to make each method no more than 10 lines long. This will force you to break things up. Right now it's a wall of code/text.

                • Typically folks would return an Enumerable<T> rather than an Enumerator – is there any reason why an enumerator is returned?
                  - Avoid deep nesting. It could take a very long time as N increases.





                share|improve this answer



























                  up vote
                  3
                  down vote













                  Some very general comments:



                  I would suggest that the code needs to be more readable, because for someone who is not familiar with it - it’s difficult to discern what is going on.



                  • You can immediately improve the code just by giving better names for variables/methods. What is tzPlayInfo? What is its instance?

                  • Why are we getting the children of children of children: this.transform.GetChild(0).GetChild(1).GetChild(1).GetChild(2).gameObject; - surely there’s a better way? What is gameObject - can you think of a more descriptive name. Also this looks like it's violating the law of demeter (please google it if you're not familiar with it.)

                  • There are way too many if statements for it to be understandable - so perhaps rethink the algorithm.

                  • I recommend breaking up the code into many separate methods - i.e. a good rule of thumb, as an exercise, would be to make each method no more than 10 lines long. This will force you to break things up. Right now it's a wall of code/text.

                  • Typically folks would return an Enumerable<T> rather than an Enumerator – is there any reason why an enumerator is returned?
                    - Avoid deep nesting. It could take a very long time as N increases.





                  share|improve this answer

























                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Some very general comments:



                    I would suggest that the code needs to be more readable, because for someone who is not familiar with it - it’s difficult to discern what is going on.



                    • You can immediately improve the code just by giving better names for variables/methods. What is tzPlayInfo? What is its instance?

                    • Why are we getting the children of children of children: this.transform.GetChild(0).GetChild(1).GetChild(1).GetChild(2).gameObject; - surely there’s a better way? What is gameObject - can you think of a more descriptive name. Also this looks like it's violating the law of demeter (please google it if you're not familiar with it.)

                    • There are way too many if statements for it to be understandable - so perhaps rethink the algorithm.

                    • I recommend breaking up the code into many separate methods - i.e. a good rule of thumb, as an exercise, would be to make each method no more than 10 lines long. This will force you to break things up. Right now it's a wall of code/text.

                    • Typically folks would return an Enumerable<T> rather than an Enumerator – is there any reason why an enumerator is returned?
                      - Avoid deep nesting. It could take a very long time as N increases.





                    share|improve this answer















                    Some very general comments:



                    I would suggest that the code needs to be more readable, because for someone who is not familiar with it - it’s difficult to discern what is going on.



                    • You can immediately improve the code just by giving better names for variables/methods. What is tzPlayInfo? What is its instance?

                    • Why are we getting the children of children of children: this.transform.GetChild(0).GetChild(1).GetChild(1).GetChild(2).gameObject; - surely there’s a better way? What is gameObject - can you think of a more descriptive name. Also this looks like it's violating the law of demeter (please google it if you're not familiar with it.)

                    • There are way too many if statements for it to be understandable - so perhaps rethink the algorithm.

                    • I recommend breaking up the code into many separate methods - i.e. a good rule of thumb, as an exercise, would be to make each method no more than 10 lines long. This will force you to break things up. Right now it's a wall of code/text.

                    • Typically folks would return an Enumerable<T> rather than an Enumerator – is there any reason why an enumerator is returned?
                      - Avoid deep nesting. It could take a very long time as N increases.






                    share|improve this answer















                    share|improve this answer



                    share|improve this answer








                    edited Jan 16 at 22:34


























                    answered Jan 16 at 11:42









                    BKSpurgeon

                    95129




                    95129




















                        up vote
                        1
                        down vote













                        • Why do you need to return an enumerator? It's only useful if you yield return items to make a list. Here you just return null which is the same as returning true or false.


                        • Why do you need the nested loop? What are you trying to do? Right now when each of the lists first item is not null, you already set the variable allFound to true but you keep on looping. So if the next item is null, you still set it to allFound. Seems strange.


                        • Naming. GetChild().GetChild(). Much easier to read as GetTest(). It's also known as a "train wreck", all the nested GetChilds. One solution is to have a method in the top class return all the data you need.






                        share|improve this answer



























                          up vote
                          1
                          down vote













                          • Why do you need to return an enumerator? It's only useful if you yield return items to make a list. Here you just return null which is the same as returning true or false.


                          • Why do you need the nested loop? What are you trying to do? Right now when each of the lists first item is not null, you already set the variable allFound to true but you keep on looping. So if the next item is null, you still set it to allFound. Seems strange.


                          • Naming. GetChild().GetChild(). Much easier to read as GetTest(). It's also known as a "train wreck", all the nested GetChilds. One solution is to have a method in the top class return all the data you need.






                          share|improve this answer

























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            • Why do you need to return an enumerator? It's only useful if you yield return items to make a list. Here you just return null which is the same as returning true or false.


                            • Why do you need the nested loop? What are you trying to do? Right now when each of the lists first item is not null, you already set the variable allFound to true but you keep on looping. So if the next item is null, you still set it to allFound. Seems strange.


                            • Naming. GetChild().GetChild(). Much easier to read as GetTest(). It's also known as a "train wreck", all the nested GetChilds. One solution is to have a method in the top class return all the data you need.






                            share|improve this answer















                            • Why do you need to return an enumerator? It's only useful if you yield return items to make a list. Here you just return null which is the same as returning true or false.


                            • Why do you need the nested loop? What are you trying to do? Right now when each of the lists first item is not null, you already set the variable allFound to true but you keep on looping. So if the next item is null, you still set it to allFound. Seems strange.


                            • Naming. GetChild().GetChild(). Much easier to read as GetTest(). It's also known as a "train wreck", all the nested GetChilds. One solution is to have a method in the top class return all the data you need.







                            share|improve this answer















                            share|improve this answer



                            share|improve this answer








                            edited Jan 16 at 17:10









                            t3chb0t

                            32.1k54195




                            32.1k54195











                            answered Jan 16 at 16:42









                            Carra

                            1864




                            1864






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185196%2f4-level-deep-of-for-loops-for-counting-objects%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                Chat program with C++ and SFML

                                Function to Return a JSON Like Objects Using VBA Collections and Arrays

                                Will my employers contract hold up in court?