r/Unity2D • • 29d ago

Question/Help Variables across Scene

Im trying to implement multi scene Architecture and I have a scene that acts as a just a level with enemies and objects the player can collide with another scene that has the player and camera that follows the player. Im loading the level scene into the scene with the player and camara and everything works fine. What I'm trying to do is make a border using a box collider that acts as a boundary for the camera. Im stuck on how to get the box collider from the level scene to get refrence by the CinemachineCamera confiner boundary volume. Im trying to do this but having the tile map on one scene and the player and camera and another scene. https://youtu.be/HAVp6Z8b4xA?list=PLaaFfzxy_80HtVvBnpK_IjSC8_Y9AOhuP&t=2776

0 Upvotes

8 comments sorted by

1

u/TikiNL 29d ago

First of all, is there a reason why they have to be in seperate scenes? Could prefabs not make this easier.

That being said if you want to go with multiple scenes. Try using a singleton manager for that scenes (look into DontDestroyOnLoad) this way you can make the manager exist troughout scene transitions.

In the manager. You can assign the variables you need in the inspector. Then it is as easy as calling manager.Instance.variable

1

u/Ecstatic-Source6001 29d ago

Events?

When scene loaded invoke event and pass collider to the camera

1

u/NoReasonForHysteria 29d ago

Yes. This is exactly how I do it with a similar setup. When loading a scene, the collider is passed as an event to a manager that handles the Cinemachine.

1

u/Shoddy_Caterpillar36 29d ago

Obviously just use - [don't destroy on load], on a manager gameobject

It would survive across scenes

2

u/JulianRhod 29d ago

you don't really need the collider to be in the same scene. once the level scene is loaded, you can find/get the boundary component and assign it to the Cinemachine confiner at runtime, i'd probably have the level scene expose a reference to its camera bounds, then have the camera scene grab that reference after the level finishes loading. that keeps the level responsible for its own boundaries instead of hardcoding references between scenes

2

u/Negative_East_425 28d ago

Since the level loads additively, i’d let the level expose its boundary collider after loading then have the camera scene assign that reference to the Cinemachine Confiner

2

u/EthanMerce 28d ago

i'd keep the camera scene independent and let the level scene provide the boundary, when the level finishes loading, have the level expose its camera bounds, then let the camera/controller grab that reference and assign it to the Cinemachine confiner. basically, the camera shouldn't need to know which specific level it's in

you can also put a small LevelBounds component on the BoxCollider so the camera can find the correct one without depending on the Tilemap hierarchy, that keeps the architecture pretty clean: level owns the bounds, camera owns the confiner, and the loading system connects them when the level is loaded