- Home /
Unity detects three children of the current object, but only grabs one
I have the following object:
In a script attached to the TriggerableDoor1 parent object, I have the following start function:
void Start()
{
Debug.Log("Child Count: " + transform.childCount);
for (int i = 0; i < transform.childCount; i++) //Get the three children objects
{
Transform t = transform.GetChild(i);
Debug.Log(t.name);
if (t.name == "ClosedLoc" && _closeloc == null)
{
_closeloc = t.gameObject;
}
if (t.name == "OpenLoc" && _openloc == null)
{
_openloc = t.gameObject;
}
else if (t.name == "Door" && _door == null)
{
_door = t.gameObject;
}
_openloc.GetComponent<Renderer>().enabled = false; //Disable the renderers for the guides
_closeloc.GetComponent<Renderer>().enabled = false;
}
}
I am attempting to get each of the object's three children, and store them within the script. NOTE: This occurs whether I use a for loop with GetChild or foreach(Transform t in transform)
When I boot up the game, however, it only finds the first object in the list. It can be any of the three objects, but ONLY if they are the first listsed.
childCount returns three, however.
Here's the actual debug output:
It only seems to ever want to grab the first object, despite the fact that it knows there are three.
What am I doing wrong?
I'm using Unity for Linux.
Answer by Lucas_Lima_14 · Oct 26, 2020 at 12:46 PM
I checked your script and it returned all the 3 objects and disabled the openloc and closedloc, the default door is properly rendered.
public class UnityAnswersTest : MonoBehaviour
{
[SerializeField] GameObject _openloc;
[SerializeField] GameObject _door;
[SerializeField] GameObject _closeloc;
void Start()
{
Debug.Log("Child Count: " + transform.childCount);
for (int i = 0; i < transform.childCount; i++) //Get the three children objects
{
Transform t = transform.GetChild(i);
Debug.Log(t.name);
if (t.name == "ClosedLoc" && _closeloc == null)
{
_closeloc = t.gameObject;
}
if (t.name == "OpenLoc" && _openloc == null)
{
_openloc = t.gameObject;
}
else if (t.name == "Door" && _door == null)
{
_door = t.gameObject;
}
}
_closeloc.GetComponent<SpriteRenderer>().enabled = false;
_openloc.GetComponent<SpriteRenderer>().enabled = false; //Disable the renderers for the guides
}
}
I just moved the _closedloc and _openloc disable renderers lines out/after the for loop. It fill the variables first and then disable the renderers.
Your change does not appear to fix the issue. I wonder if this is a bug with the Linux implementation of Unity.
Never$$anonymous$$d! It did end up working. I wonder why disabling the renderer disabled the objects.
Your answer
Follow this Question
Related Questions
How to sync child objects of a transform in multiplayer 3 Answers
Apply changes to a random child from sublist 1 Answer
What is the order in which transforms are stored in .unity (scene) file? 1 Answer
Which is a faster find? (foreach) or (for-loop) 2 Answers
Why do we access children through an object's transform? 2 Answers