How to SetActive Object in different Scene?

Hi

I have two scenes in my game , scene NO1 has an object which is hidden , in Scene NO2 is for the game , while the player collect certain item ,I want the hidden object in scene NO1 to be activated , I tried this code but it didn’t work with me in different scenes , it just worked with one scene

using UnityEngine;
using System.Collections;

public class HiddenObject: MonoBehaviour {
    public static bool ShowHidden=false;
    public GameObject Hidden;
    public static HiddenObject GameInstance;
    private HiddenObject()
    {
        GameInstance = this;
    }

       public void Update(){
        if (ShowHidden= true) {
        Hidden.SetActive(true);
                }
    }
}
using UnityEngine;
using System.Collections;

public class CollectedItem: MonoBehaviour {
  
        void OnTriggerEnter2D(Collider2D collisionObject) {      
        if (string.Equals (collisionObject.tag, "Bucket")) {
            HiddenObject.ShowHidden=true;
          
        }


}

}

I wonder why it didn’t work in different scenes

This isnt the issue, but change if(ShowHidden=true) to if(ShowHidden == true).
The first is assigning the variable to true, thensecond is checking if it is true. You can also do if(ShowHidden) to check if its true.

I am assuming the hiddenobject component is on the hidden object, which means that object starts off disabled, meaning its update will not run. You will need some gameobject that is not hidden to activate the hidden object.

Alternatively, triggers will still fire if an object has a trigger collider, even if the GameObject is disabled. This is one of the few ways that allows GameObjects to re-enable themselves.

yes the hiddent component is Hidden Object, there is no problem with the code i think because i mentioned the code is work fine in the same scene , that hidden object is being active while i collect the certain item , but it is not working if that hidden object is exist in different scene , i think the collected item script is not seeing the hidden object script or something :face_with_spiral_eyes::roll_eyes:

You can’t interact directly with objects in other scenes. They don’t exist until the scene is loaded.

You can use any number of techniques from the learn section video on saving and persistiant data to produce scene to scene interactions.

BoredMormon is talking about

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

Which will help you alot, I use something like this as well…works great…

1 Like