How to access Instantiated gameObjects from a Collection inside for loop?
Asked Answered
S

1

0

I have 3 prefabs named "blackout" being instantiated in the position of other 3 gameObjects I positioned and rotated myself in the editor. This works fine.

However, after the 3 blackouts are instantiated, I need to "set active to false" (disactivate) each one of them in an interval of 2 seconds between each one - starting from the first blackout - instantiated in the position of blackoutCollection[0] - until the last and third blackout - positioned in blackoutCollection[2].

How can I do this? I'm failing to access the blackout gameObjects. blackoutCollection *are only empty gameObjects used to place the blackout prefabs where I want.

* *```* *function Blackout()* *{* *var i : int = 0;* *for (i=0; i<=2; i++)* *{* *Instantiate (blackout,* _Vector3 (blackoutCollection*.transform.position.x,*_ _blackoutCollection*.transform.position.y,*_ _blackoutCollection*.transform.position.z),*_ _blackoutCollection*.transform.rotation);*_ _*}*_ _*yield WaitForSeconds(2);*_ _*blackoutCollection[0].gameObject.active = false;*_ _*// Lines like the above represent what I have,*_ _*// but I don't need to disactivate those empty gameObjects.*_ _*// I need to do this with the blackout prefabs.*_ _*yield WaitForSeconds(2);*_ _*blackoutCollection[1].gameObject.active = false;*_ _*yield WaitForSeconds(2);*_ _*blackoutCollection[2].gameObject.active = false;*_ _*}*_ _*```*_ _*

I tryied using `blackout.blackoutCollection[1].gameObject.active = false;` but of course that didn't work.

*_
Serbocroatian answered 3/7 at 8:50 Comment(0)
I
0

you can each instantiated prefabs set child your 3 position gameobject (blackoutCollection*)

* *

try follow. not checked

* *

for (i=0; i<=2; i++)* *{

* *```* *// add this* *var instObj = Instantiate (blackout,* _Vector3 (blackoutCollection*.transform.position.x,*_ _blackoutCollection*.transform.position.y,*_ _blackoutCollection*.transform.position.z),*_ _blackoutCollection*.transform.rotation);*_ _*// add this*_ _instObj.parent = blackoutCollection*.transform;*_ _*}*_ _*```*_ _*

and instead

*_
_*

blackoutCollection[1].gameObject.active = false;

*_
_*

call

*_
_*

blackoutCollection[1].gameObject.SetActiveRecursively(false);

*_
Internuncio answered 6/6, 2023 at 1:6 Comment(1)

Thank you! This worked perfectly. The only thing that was missing was the "transform.parent": instObj.transform.parent = blackoutCollection*.transform;*

Serbocroatian

© 2022 - 2024 — McMap. All rights reserved.