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. Dismiss Notice

Bug When I try to access all of the children of a GameObject, only the even numbered ones get selected

Discussion in 'Scripting' started by MrCheese556, Jul 28, 2023.

  1. MrCheese556

    MrCheese556

    Joined:
    Jan 15, 2022
    Posts:
    1
    I was working on a script for my project to sort the children of a GameObject into different parents depending on a value inside one of their scripts, but when I tested it, only the even numbered objects were working correctly. I put a Debug.Log into the for loop, to see how many times it ran, and it only ran half of the amount it was supposed to, and only selected the even numbered objects.
    Here is the code for the sorter:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class gcontroller : MonoBehaviour
    6. {
    7.     public GameObject callParent;
    8.     public GameObject[] dayControllers;
    9.     public int[][] results;
    10.     public int carNum;
    11.  
    12.     public void Calculate(){
    13.         for(int i = 0; i < callParent.transform.childCount; i++){
    14.             callParent.transform.GetChild(i).SetParent(dayControllers[callParent.transform.GetChild(i).gameObject.GetComponent<mover>().day].transform);
    15.             Debug.Log(callParent.transform.GetChild(i).gameObject.GetComponent<mover>().day);
    16.         }
    17.         for(int j = 0; j <7; j++){
    18.             dayControllers[j].GetComponent<calculator>().numClusters = carNum;
    19.             results[j]  = dayControllers[j].GetComponent<calculator>().redo();
    20.         }
    21.     }
    22. }
    Here is the picture of the result:
    l.png
    The code works correctly for the even numbered objects though.
    I've only seen one other stack overflow post about someone getting this result, but it wasn't answered, here is the link:
    https://stackoverflow.com/questions...d-in-even-positions-of-the-hierarchy-window-w
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    First of all: shorten your calls. DRY principle. Don‘t repeatedly write out things like callParent.tansform.GetChild(i). Get that child once, assign it to a variable child. This saves both time and screenspace and makes the code easier to read and maintain.

    It also makes it easier to spot issues. Such that you reparent objects as you go over the children, thus changing the list of children while you go over them. So the first game object with index 0 moves to a different parent object. the childcount is reduced by one. The gameobject that used to be at index 1 is now at index 0. the next object you access however is index 1. Therefore you skip every second object.
     
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    The usual way to go through a list where you delete objects is to start from the back:
    for(int i=childcount-1;i>=0;i--)
    . (oops! changed to i--)!
     
    Last edited: Jul 29, 2023
    ijmmai and MrCheese556 like this.