- Home /
Load Children One at a Time
Hi, I'd like to load each child of an object one at a time after an event has taken place. So far I'm only able to set the parent game object active but would like another effect, please help!
void Start ()
{
_clickanddragscript._OnMouseUp = false;
lineParent.SetActive(false);
}
void Update () {
if (firstBar.transform.position != Vector3.zero)
{
Debug.Log("HasChanged");
if (_clickanddragscript._OnMouseUp)
{
Debug.Log("Mose");
firstBar.transform.DOMove(new Vector3(0, 10.9f), 2, false);
GetMyChildren();
}
// Wait 1 second
// Move the bar up
// Add some indication juice
// Load all the children of red lines parent object and wait a little between each load
}
}
void GetMyChildren()
{
foreach (Transform child in lineParent.transform)
{
for (int i = 0; i < barArray.Length; i++)
{
lineParent.SetActive(true);
// maybe do a coroutine
}
}
}
whats the other effect that you want to achieve? you have not telled us
Oh, it's in the update function already. It's just if player has clicked and let go of mouse, move another object to a position. Now I need help loading all children of a parent one at a time.
foreach (Transform child in lineParent.transform){ child.gameObject.SetActive(tue);}
this?
Answer by David_Rios · Mar 29, 2019 at 07:02 PM
Instead of creating a function for GetMyChildren() you can make a coroutine. It'd be a much cleaner way of activating all your children one-by-one.
This function would activate one of lineParent's children after a certain delay of seconds; I believe this is the effect you want.
Here's an example of a way you could do so with an adjustable time delay:
IEnumerator GetMyChildren(float delay)
{
lineParent.SetActive(true);
foreach (Transform child in lineParent.transform)
{
yield return new WaitForSeconds(delay);
child.SetActive(true);
}
}
If you wanted a delay of one second, for example, you could call the function in your 'if' statement in the Update function like this: StartCoroutine(GetMyChildren(1f));
Sorry if I misjudged what effect you wanted to achieve, if so, I'll fix my answer, just specify below in a comment.
Thanks @BDCakerules! This was helpful. The thing I noticed is that I had to change the child from a transform to a gameobject to set it active, but it didn't work so I did it manually ins$$anonymous$$d and it worked!
IEnumerator NewGet$$anonymous$$yChildren() {
for (int i = 0; i < all$$anonymous$$yChildren.Length; i++)
{
yield return new WaitForSeconds(delayTime);
lineParent.SetActive(true);
all$$anonymous$$yChildren[i].SetActive(true);
}
}
@Smujukia, ins$$anonymous$$d of changing the child you can just access its gameObject component with .gameobject: Ex:
all$$anonymous$$yChildren[i].gameObject.SetActive(true);
This will work even if your children are transforms. If my answer helped, please mark it as correct! I'm happy you're getting the effect you want.