Set light to active depending on if a specific game object is active in different scene once this scene ends not working

Hi,

In my main scene there are 36 lanterns, which can be set to inactive whilst playing the game. (though some will remain active once the game has ended, depending on which colliders are hit)

When the title scene loads there are 36 corresponding lights.

On loading the title scene, I want only the lights to be set to active which correspond to the lanterns in the main scene which were still set to active when the game ended.
The lights which correspond to the lanterns which were set to inactive whilst playing the game in the main scene, I want to be set to inactive for 5 seconds, and then to also be set to active.
(so after 5 seconds - all 36 lights in the title scene are active again)

I attached a dontdestroyonload script in the main scene, but I’m struggling to get my script working in the title scene.

Any ideas on what I’m doing wrong?

Title scene script:

var light1 : GameObject;

function Start () {

 if(GameObject.Find("LanternA3A4").activeInHierarchy == true) {
 
 light1.active = true;
 
 }
 
if(GameObject.Find("LanternA3A4").activeInHierarchy == false) {
light1.active = false;

yield WaitForSeconds (4);

light1.active = true;

yield WaitForSeconds (1);

Destroy (GameObject.Find("LanternA3A4"));
}

}

Main scene script:

var lantern1 : GameObject;

function Update() {
	DontDestroyOnLoad(lantern1);

}

(I’ve just shown the script for 1 light, rather than all 36, so it’s not too long to see here)

Best, Laurien

GameObject.Find will only successfully find active game objects. Since you’re trying to find an inactive game object it won’t return anything. I’d only do the GameObject.Find once and store the value (that’s more efficient anyways). If the value you stored is null then in this example it means the light was inactive in the last scene.

Note: This isn’t a totally safe approach because not finding the game object could also mean that it just didn’t exist or that the name got changed. There’s other ways you could capture what lights were on or off that would be safer and more efficient than GameObject.Find.