How can I attach a variable of a certain gameObject to another gamaObject in different scene?

Hello everyone,

I created a script with a dynamic variable. This script is assigned to the GameManager of scene one. I want to take the object given to this dynamic variable, save it in another variable of a different script, and attach it to another object of scene 2.

How can I do this?

Static locators can work if it is a single object and you will never have more than one.

// ultra-simple monobehaviour static locator:
public static MyExampleClass Instance { get; private set; }
void OnEnable()
{
   Instance = this;
}
void OnDisable()
{
   Instance = null;   // keeps everybody honest when we're not present
}

NOTE: the above does NOT address lifecycle.

If you will have many, you may need a more-flexible service locator approach.

The built-in FindObjectsOfType() form an excellent basis for any sort of service locator.

Here’s some other ideas:

ULTRA-simple static solution to a GameManager:

OR for a more-complex “lives as a MonoBehaviour” 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.

And if you really insist on barebones C# singleton, here’s a Highlander: