Transitioning scene and enabling a certain camera and disabling another one. (790869)

As you see in the screenshot below Im making a script where I transition from one scene to another and there are multiple cameras in that scene so when I trigger the collider it activates a certain camera in that scene and disables the other ones, therefore so I can have every interior in one scene and the city in the other scene. The problem is when I trigger the collider it gives me the message error that Object reference not set to an instance of an object when its supposed to be referenced by the void update gameobject find function.

Scenes do not load until after the end of the current frame, so you can’t load it and then operate on things you expect to be in the scene.

You can make a coroutine to yield return null; one frame, THEN act on it, but now you have to make sure that coroutine is on an object that is marked as DontDestroyOnLoad() so that it persists into the next scene, then you can Destroy it afterwards.

A more general case is to have a GameManager that has a portion of itself dedicated to knowing what condition the scene has to be in and waits for it to finish loading, then sets those conditions. Via the Unity SceneManager you can subscribe to delegates to tell you when a scene is finished loading, and do your conditioning at that point.

Can I send you 5 dollars on Pay Pal for you to show me how to do that because I feel like ill get something wrong.5849185--621253--Screenshot (8).png
as you see in the bottom right I already had a dont destroy object script on it, so what I expected to happen was the void update to still run after the scene was loaded so it can fill those game objects but i was wrong. If you show me what you just told me Ill literally give you 5 bucks on Pay Pal

Are you sure the camera from the previous scene still exists when you try to disable it? Are you loading this new scene additively or replacing the current scene? I don’t see any GameObject in the hierarchy image which matches exactly the names you are searching for in your GameObject.Find calls. If they aren’t exactly matching, and aren’t currently in the running scene when GameObject.Find is called (which you’re calling every frame), then GameObject.Find will return null.

Don’t call GameObject.Find every frame by the way. It is a performance hog.

The camera is not in the City scene its in the scene im loading to, and its disabling the other camera in the scene im loading to so only one camera will be active because there will be multiple cameras in that scene for each interior.

I wouldn’t design it this way. Just have a camera manager already in the next scene, with references to all the cameras already set via inspector instead of finding them, and have the appropriate camera already enabled and the other ones already disabled. If you need to switch cameras you just talk to the camera manager.

i tried looking up “camera manager in unity”, nothing showed up. I have no idea what that is.

It is just a script you would write and attach to a GameObject in the scene to handle managing of the various cameras. Attach to an object other than the camera objects, which you know will be active when needed. Quick example off the top of my head below. Might have bugs, as I just typed it directly into the forum.

public class CameraManager : MonoBehaviour
{
    public Camera ClickerCamera;
    public Camera SampleCamera;

    public enum CameraSelection {Clicker, Sample};

    void Start()
    {
        EnableCamera(CameraSelection.Clicker);
    }

    public void EnableCamera (CameraSelection enableThisCamera)
    {
        switch (enableThisCamera)
        {
            case CameraSelection.Clicker:
                ClickerCamera.gameObject.SetActive(true);
                SampleCamera.gameObject.SetActive(false);
                break;
            case CameraSelection.Sample:
                ClickerCamera.gameObject.SetActive(false);
                SampleCamera.gameObject.SetActive(true);
                break;
        }
    }
}

alright this seems very helpful thank you.