Splitting project between multiple scenes

I have a project I have been working on for a while, and I want to split the Environment and player and UI into different scenes.

When I move the player, camera, and UI to a different scene, every object that referenced the player does not work and the game cannot start. I obviously don’t know what I’m doing.

Does anybody know how to reference an object in another scene?

I’m assuming you’re loading multiple scenes at the same time in Additive mode,
You can use SceneManager.GetSceneByBuildIndex to get the scene object, and use Scene.GetRootGameObjects to find the gameobject you want.

If the scenes are being loaded additively, then there is no way to set the references ahead of time; you will have to dynamically find the references at runtime. Ideally, each of these scenes are mostly self contained and don’t need too many references outside of their own scene. If there is too much direct interconnect, then maybe they should not be split into separate scenes.

One option is to use FindObjectOfType. This can work if there is only one instance of the thing you are looking for in the other scene.

You could also use static references. This lets other classes get a reference to an object without having to search. For example:

public class StaticExample : MonoBehaviour
    public static StaticExample instance;

    private void Awake()
        instance = this;
//other classes will be able to access this object using

This also only works if there is only a since instance of this class in the scenes you have loaded. You’ll also need to be careful about stale references when this object is destroyed or unloaded.

To reduce the number of searches you need to do, you could create a script to collect all of the references that may be needed by other scenes. This will let you set the references in the editor and only need to do a single search (or static reference) at runtime. For example:

public class ReferenceLibrary : MonoBehaviour
    public PlayerMovement player;
    public DialogueManager dialogueManager;
    public GameNode gameNode;

If each scene is going to search for objects in the other scenes, then you’ll need to make sure they don’t start searching until the other scene has loaded. You may be able to resolve this by controlling which order the scenes load, or you may need some Manager to advertise when all scenes have been loaded so that objects can run their initialization. This can be done with a delegate in your script which handles scene loading.

Thank you for your help! I will implement this.

How would I assign a GameObject that is not the gameobject the script is on?

I am referencing a boss object through this script, static gameobject boss. When I set the boss to the player’s boss object in the player script, it works fine. Then when I try to access the bosses script by assigning the script variable to the boss by writing

boss = GameData.boss
bossscript = boss.GetComponent<bossscript>().

it creates a compiler error (null reference) here and on any line that uses the boss script
Do you know how to fix this?

This isn’t the exact code I use, the boss is a bird made of magma and I use different names like magmaboss, boss, theboss, magmabird, so I do not believe this is the issue.

I have figured out the problem. The boss by default is deactivated in the scene and only activates when the player enters its arena. I have fixed all issues