- Home /
How to find child object, without name or tag.
Hello,
Before I explain my issue, I'd like to point out that this isn't a "ohmigosh I'm stuck I cannot continue without help ever" I can very easily continue; there is an easy-ish workaround, but it ain't pretty.
I'm working on a group project, and I'm creating a chandelier that's supposed to be able to have its rope burnt whenever our main character attacks it. I'm using parent + child to "know" which part of the rope is above, and which part is below the targeted rope.
The top-most is an immovable, empty game object for easy implementation.
the script I'm working with right now is as follows:
private GameObject RopeAbove;
public GameObject RopeBelow;
private bool IsBurning;
// Use this for initialization
void Start () {
RopeAbove = transform.parent.gameObject;
Debug.Log(RopeAbove);
Debug.Log(RopeBelow);
}
Of course, this works, but it's not a "pretty" solution. I'd like to know if you can call a child in the same way I have called the parent.
Thanks in advance!
Answer by WesleyWorks · Oct 25, 2018 at 11:21 AM
@Vicarian helped me out; however, there was a problem with using "transform.GetChild(0)", instead you should use "transform.GetChild(0).GameObject". This will make it return the gameobject instead of the transform, which is what I wanted.
Fixed code:
private GameObject RopeAbove;
private GameObject RopeBelow;
private bool IsBurning = false;
// Use this for initialization
void Start () {
RopeAbove = transform.parent.gameObject;
Debug.Log(RopeAbove);
if (transform.childCount > 0){ // Prevents ArgumentOutOfRangeException
RopeBelow = transform.GetChild(0).gameObject;
}
Debug.Log(RopeBelow);
}
// Update is called once per frame
void Update () {
if(IsBurning == true){
//RopeAbove.BurningScript.IsBurning = true;
//RopeBelow.BurningScript.IsBurning = true;
}
}
Answer by Vicarian · Oct 23, 2018 at 02:09 PM
Looks like each individual transform has only one child, so transform.GetChild(0)
ought to work:
if (transform.childCount > 0) // Prevents ArgumentOutOfRangeException
RopeBelow = transform.GetChild(0);
If you reference the RopeBelow object from script, you'll need to make sure it has a value before accessing its members:
if (RopeBelow) // Prevents NullReferenceException
// Do stuff with RopeBelow
Hi, for me when I attempt to use "if (transform.GetChildCount() > 0" Visual Studio code gives me an error:
'Transform.GetChildCount()' is obsolete: 'warning use Transform.childCount ins$$anonymous$$d (UnityUpgradable) -> Transform.childCount' [Assembly-CSharp]
Sorry for taking so long to respond.
Apologies, use
if (transform.childCount > 0)
Ins$$anonymous$$d. I went looking for that property in the docs but must've overlooked it.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Access variable in parent script C# 4 Answers
Distribute terrain in zones 3 Answers
Get Children of Child and Deactivate/Activate on Demand 1 Answer
How can I listen for a function being called from a parent class? 1 Answer