Transfer a gameobject between specific scenes

I am trying to implement something but i can’t find a solution.

What i want to do is.

  • There is two scenes: CharacterScene and GameScene
  • CharacterScene has a gameobject “player” and a button
  • When i click the button, GameScene will be loaded

The button includes this code

public class StartTheGame : MonoBehaviour
{
    public void LoadGame()
    {     
        GameObject player = GameObject.Find("Player");

        if (player != null)
        {
            SceneManager.LoadScene("GameScene");

            DontDestroyOnLoad(player);
        }
    }
}

The problem is this

  • When i click the button, player gameobject will perfectly transfered to GameScene but
  • When i return to CharacterScene there is 2 player gameobjects

The character will be fully customizable so having two player will be a problem.
Also, the player is staying to any scene i change.

How can i implement the player transfer to specific scenes?
Also how can i keep only one player gameobject when i return to CharacterScene?

The above is a faulty singleton implementation because it uses GameObject.Find(), and probably for other reasons too.

Remember also that scenes do not load until the next frame, but that may not matter here.


Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: Regarding GameObject.Find · UnityTipsRedux

More information: Why cant i find the other objects?


Here is some singleton / GameManager pattern that I frequently use:

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!

The above solutions can be modified to additively load a scene instead, BUT scenes do not load until end of frame, which means your static factory cannot return the instance that will be in the to-be-loaded scene. This is a minor limitation that is simple to work around.

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.

And finally there’s always just a simple “static locator” pattern you can use on MonoBehaviour-derived classes, just to give global access to them during their lifecycle.

WARNING: this does NOT control their uniqueness.

WARNING: this does NOT control their lifecycle.

public static MyClass Instance { get; private set; }

void OnEnable()
{
  Instance = this;
}
void OnDisable()
{
  Instance = null;     // keep everybody honest when we're not around
}

Anyone can get at it via MyClass.Instance., but only while it exists.


And here are some notes on additive scene loading is one possible solution:

A multi-scene loader thingy:

My typical Scene Loader:

Other notes on additive scene loading:

Timing of scene loading:

Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It’s a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

Two similar examples of checking if everything is ready to go:

1 Like

@Kurt-Dekker Do you have a bunch of pre-written posts that you copy-paste? Or do you just use some AI you’ve trained on your own posts?

Or are you actually typing out an essay like 30 times a day?

I do!

Besides helping others, the main point of me doing this is for ME to learn interesting and new things about Unity.

This works really well in fact!

But there is a huge corpus of posters with mundane issues (NullRef) or else those who have gone far beyond their basic understanding of Unity object / asset lifecycles, and that’s where the above copy / pasta comes from.

You will note that in all but the most-trivial “I have error CS#### halp meee!!!” type of posting, there is often a sentence or two that I write at the top.