Need advice on best way to deal with scene changes in a card game:

I’m building a card game and, so that I don’t crowd the screen, I’d like to utilize scene changes that allow the player to view certain subsets of cards (e.g. the discard pile).

The way my game is structured, card game objects are contained in lists (e.g. “Hand” is a list that contains the cards in your hand), and all the lists and game-keeping variables are declared and initialized in a gameController object.

Based on some research, I’ve been playing around with the DontDestroyOnLoad() function. However, I’m still not sure how to get the results I want - upon switching scenes to the discard pile and returning to the original scene, the cards in “Hand” are all gone. Some approaches I’ve tried:

  1. Using DontDestroyOnLoad on the gameController object alone. The issue was that, on changing scenes, the lists of cards (e.g. “Hand”) would all contain “Missing” references to the card game objects - since all the cards were destroyed.
  2. Using DontDestroyOnLoad on every game object, and (de)activating their renderer / collider components to control whether they appear in a scene. I never got this to work, and it seemed like a sloppy solution anyway.

I would really appreciate some advice on how to change scenes properly.

Thanks in advance for your time.

My personal preference to retain objects across scenes is to assign them to a global variables.

Create a script named something like ‘GlobalVariables’

and create your hand list there:

public static List hand;

Since the list is static, you should be able to assign and retrieve the values across your scenes by using class reference. Eg:

GlobalVariables.hand