Changing Scene 2's Cameras from Scene 1

Hello, I’m working on a project which contains three different cameras in game scene. I also have a camera on my menu scene. What I want to do is, there are 3 buttons on the main menu scene. All of them gets me to the game scene with no problem. But I want each of them to activate different cameras. I know how to change cameras in a scene but, I should change the game scene cameras from the main menu scene buttons. I’m new to unity and tried to get together some code for it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CharSelect : MonoBehaviour
{
    public GameObject ThirdCam;
    public GameObject FirstCam;
    public GameObject TopCam;
    public static int CamMode;

    IEnumerator CamChange()
    {
        yield return new WaitForSeconds(0.01f);
      
        if(CamMode == 0)
        {
            ThirdCam.SetActive(true);
            FirstCam.SetActive(false);
            TopCam.SetActive(false);
        }

        if(CamMode == 1)
        {
            FirstCam.SetActive(true);
            ThirdCam.SetActive(false);
            TopCam.SetActive(false);
        }

        if (CamMode == 2)
        {
            TopCam.SetActive(true);
            FirstCam.SetActive(false);
            ThirdCam.SetActive(false);
        }
    }

    public void CharSelected ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
  
}

CharSelected functions works with no problem, but I can’t drag&drop cameras in editor to GameObject classes. What should I do?

8636679--1161378--upload_2022-12-5_17-29-33.png

Steps to success:

  • decide what you want on / off (as you have already)

  • store that data somewhere persistent (see below)

  • act on it when the next scene is actually loaded (you can’t until it is loaded because those cameras don’t exist)

Storing persistent stuff generally requires a GameManager. Look online for bazillions of tutorials / talks about those.

ULTRA-simple static solution to a GameManager:

OR for a more-complex “lives as a MonoBehaviour or ScriptableObject” solution…

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

OR just make a custom ScriptableObject that has the shared fields you want for the duration of many scenes, and drag references to that one ScriptableObject instance into everything that needs it. It scales up to a certain point.

Thanks for the detailed answer. But I sadly didn’t really understand most of it. And I realized my problem is probably where you said “can’t make changes on unloaded scene” which is what I’m trying to do from the start.

And another point is, I will not be constantly changing cameras, once a camera is selected, it will stay on that camera through the game. And I will make other more complicated scripts based on active camera, like character stats etc. So I’m not sure if should use Singleton on that.

My actual problem here is I can’t reach my scene 2 cameras from scene 1. I should most probably use this method to prevent future problems.

That gives you a great starting point to begin your research. This is not new information. People have been saving games for sixty or seventy years now, so if you want to play, it behooves you to dig into it.