Scene merging, reference, DontDestroy scripts problem

So scene dividing is best for performance?

Then problem is, scripts that should be permanent between scenes.

And above script’s variable’s reference connected to hierarchy’s gameobject.

Then how to do it between scenes?

Make hierarchy’s referenced gameobjects as prefab and connect script variable to that prefab?

or using GameObject.Find to get reference after move to new scene?

or other good way?

Thanks.

I don’t think it necessarily impacts performance. It’s just a great organization benefit to have additively-loaded scenes.

Additive scene loading does not change any of the Unity rules about what gets cleared when a scene is unloaded.

The primary cost to making additive scenes is slightly-higher awareness of object lifetimes.

For instance if you have two scenes:

  • player
  • the level including enemies.

When the player dies I find it easiest to just unload his scene, then load it afresh.

Upon loading the player scene does nothing until it is able to find a spawnpoint.

The spawnpoint would be in the level scene (and could even be dynamic, like unlockable spawnpoints you must reach).

Upon loading the enemies do nothing because they cannot find the player.

This back-and-forth generally just requires some extra checks here and there.

Additionally remember that instantiated objects are placed in the active scene. You can move them around if you like.

1 Like

Here my team’s programmer consists of 4 programmer, 1 lobby, 1 adven, 1 battle, 1 freeroll(me).

So dividing scene (lobby, adven, battle) is for each of them work easily and not conflict when using Unity Collaborate.

Additive scene loading? How to apply it in this case then?

Smart!

Didn’t you and I already talk about this before? :slight_smile:

Additive scenes are one possible solution:

https://discussions.unity.com/t/820920/2
https://discussions.unity.com/t/820920/4

I would just use a GameManager construct to find everything, something that lazily comes into being that “interesting” parts of each scene can register with, perhaps with methods like:

// in the GameManager:
public void RegisterThePlayer( GameObject player)
{
}

public bool IsPlayerPresent()
{
  return true / false accordingly
}

public GameObject FindMeThePlayer()
{
  return (the player if it exists)
}

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

https://pastebin.com/SuvBWCpJ

Unity3D Singleton with Prefab used for predefined data:

https://pastebin.com/cv1vtS6G

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
}