Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] Creating list on runtime while check if it returns null

Discussion in 'Scripting' started by chrisoooooooooooooooo1, Feb 28, 2016.

  1. chrisoooooooooooooooo1

    chrisoooooooooooooooo1

    Joined:
    Mar 30, 2015
    Posts:
    53
    Hi guys,

    im trying to create a list of all the childs, and while it is created i want to check if the list turns out to be null.

    Take a look please:
    Code (CSharp):
    1. void Update () {
    2.         if (GM.gameprogress == activeAt) {
    3.             active = true;
    4.             int i = 0;
    5.  
    6.             List<GameObject> children = new List<GameObject>();
    7.             foreach (Transform child in transform) {
    8.                 if (child.gameObject.layer == 27) {
    9.                     child.gameObject.SetActive (true);
    10.  
    11.                     children.Add(child.gameObject);
    12.  
    13.                 } else {
    14.                     child.gameObject.SetActive (false);
    15.                 }
    16.  
    17.  
    18.             }
    19. //THIS PART DOES NOT WORK
    20.             if(children == null){
    21.                 active = false;
    22.                 GM.gameprogress += 1;
    23.             }
    24. //
    25.         }
    26.     }
    So in my Game I have a gameobject with lets say 2 childs, as soon as those childs are destroyed the list should return null to set the gameprogress +1 stuff.

    Can anyone enlighten me?

    THank you Guys!
     
  2. Gohan1

    Gohan1

    Joined:
    Nov 20, 2015
    Posts:
    28
    just because you dont have children doesnt mean your existence is void. you should check the children count children.count <= 0. or check if all children are null.
     
  3. chrisoooooooooooooooo1

    chrisoooooooooooooooo1

    Joined:
    Mar 30, 2015
    Posts:
    53
    Excellent!! It does work!

    Thank you very much.