- Home /
how to use transform.GetChild(0) in a function ?
so I'm able to find children of a gameobject by their index number with above function, but I dont know how to call a particular child by their index number.
like I want to do something like this
if childA is disabled, enable childB
if childB is disabled, enable childC
if childC is disabled, and there is no childD, enable childA again
and the loop goes on forever
also I'm noob at programing, so if you could explain your function, that would be very helpful, I want to learn, not just copy paste.
Answer by FernandoHC · Aug 09, 2018 at 11:38 AM
I'm actually having a really hard time understanding what you want to do. So I will assume you only want to enable objects, this is what you want then: for iterates through all childs, then checks if the current child is disabled, if so enables the next one.
public void OnTargetHit()
{
gameObject.SetActive(false);
if (transform.GetSiblingIndex() < transform.parent.childCount - 1)
transform.parent.GetChild(transform.GetSiblingIndex() + 1).gameObject.SetActive(true);
else
transform.parent.GetChild(0).gameObject.SetActive(true);
}
hi sorry english is not my first language. let me explain again.
so there's a bullet that when hits a target(1st child of empty gameobject),
that 1st target disappears and 2nd target appears, when bullet hits 2nd target it disappears and 3rd target appears. when last target disappear and there are no further targets left to shoot, 1st target appears again.
am I being clear now?
Ok, I have updated the code, you should put that on the bullet detection part of your code, on the child/target object.
yep, that worked beautifully. thanks. :).
Answer by Bunny83 · Aug 09, 2018 at 12:31 PM
Ok based on your comment your question has completely changed and is much clearer now ^^. Things like that shouldn't be handled at a central point because you have to keep track of many things at the same time. Also checking if an object is disabled every frame is just bad design.
Instead you can create a very simply script which you attach to each target.
// Target.cs
using UnityEngine;
public class Target : MonoBehaviour
{
public GameObject Next;
private void OnCollisionEnter(Collision collision)
{
gameObject.SetActive(false);
if (Next != null)
Next.SetActive(true);
}
}
Just disable all targets in the scene except the first one. Now you can link your targets together in whatever order you like by dragging the "next" target which should be active onto the "Next" field in the inspector. You can also link the last target simply back to your first one.
If you don't want to do the linking by hand you can create another script on the parent which just does the linking for you:
void Start()
{
if (transform.childCount <1)
{
Debug.LogWarning("not enough children to link them");
return;
}
GameObject last = transform.GetChild(0).gameObject;
for(int i = transform.childCount-1; i >= 0; i--)
{
Target target = transform.GetChild(i).GetComponent<Target>();
if (target != null)
{
target.Next = last;
last = target.gameObject;
}
}
}
hi thanks for the comment.
your 2nd script for parent shows an error for (GameObject last = transform.GetChild(0);) cannot convert gameobject into transform
for (target = last;) can not convert gameobject into target.
and your 1st script, I manually assigned all the targets, it makes 1st target disappear when bullet hits, but 2nd target doesnt reappear
$$anonymous$$y bad, there where two small errors ^^. I've edited my answer. The changed lines are:
GameObject last = transform.GetChild(0).gameObject;
for(int i = transform.childCount-1; i >= 0; i--)
and
target.Next = last;
Note the .gameObject
at the end as well as the childCount-1
in the for loop. After the target i was missing the .Next
. I've written this just quick and dirty here ^^.
In order for the first script to activate the next, the "Next" field has to be linked to the next target object. As i said you can do this in the inspector manually by dragging the next target which should be activated onto the Next field, or use the second script (which hopefully works now ^^).
hi, I tried your script now, it works perfect. but I do have to tell you that script mentioned by fernando is a litter better ;) so I used his method, hope that doesnt upset you. thank you for answering my question :)