PurrDiction

Scenes

PurrDiction runs one prediction world per Unity scene. The PredictionManager in a scene is that scene's world: it only knows about the predicted objects in its own scene, and it only steps that scene's physics.

One PredictionManager per scene

Put the PredictionManager in the gameplay scene it simulates. Do not move it to DontDestroyOnLoad.

The manager works off the scene it lives in (gameObject.scene):

  • It registers itself under that scene's handle. Components such as LocalPhysics look it up from their own scene.
  • It discovers scene-placed predicted identities in its own scene only.
  • hierarchy.Create instantiates new objects into its own scene.
  • It steps its own scene's physics scene (scene.GetPhysicsScene() / GetPhysicsScene2D()).
  • Physics callbacks resolve the other object through the manager. An object the manager does not track resolves to a null other.

If the manager sits in DontDestroyOnLoad while the gameplay objects live in a loaded scene, none of this lines up: the scene's predicted objects are not part of its world, and collision and trigger callbacks report a null other.

The NetworkManager can stay in DontDestroyOnLoad. That is the usual setup, and PurrNet's scene module needs it when you load scenes in Single mode. Only the PredictionManager belongs in the gameplay scene.

Loading a predicted scene

Load gameplay scenes on the server through PurrNet's Scene Module, so every client loads the same scene and the scene's PredictionManager spawns on all of them.

When you load a predicted scene additively, give it its own physics scene with physicsMode:

using PurrNet.Modules;
using UnityEngine.SceneManagement;

// On the server
networkManager.sceneModule.LoadSceneAsync("Game", new PurrSceneSettings
{
    mode = LoadSceneMode.Additive,
    physicsMode = LocalPhysicsMode.Physics3D, // LocalPhysicsMode.Physics2D for a 2D game
    isPublic = true
});

Without a local physics scene, an additive scene shares Unity's default physics scene with every other scene that does not have its own. The manager would then step and resimulate all of those bodies, including ones outside its world, and two predicted scenes loaded at the same time would step the same physics twice per tick. With LocalPhysicsMode each scene's bodies only collide with, and are only simulated by, their own scene.

A single gameplay scene loaded in Single mode can use the default physics scene, since nothing else shares it.

A PredictionManager that uses Unity physics switches Physics.simulationMode (and Physics2D.simulationMode for 2D) to Script when it wakes up. That setting is global, so physics in scenes without a PredictionManager stops stepping on its own while one is loaded.

Predicted objects stay in their scene

A predicted identity belongs to the world that registered it. There is no way to move it to another scene's PredictionManager, so do not call SceneManager.MoveGameObjectToScene on it.

To move something between scenes, delete it from the old scene's hierarchy and create it again in the new one:

// On the server, with a reference to each scene's PredictionManager
oldWorld.hierarchy.Delete(oldId);
var newId = newWorld.hierarchy.Create(playerPrefab, spawnPosition, spawnRotation, owner);

The new object starts fresh. Read anything you want to keep (health, inventory) before deleting, and apply it to the new object after creating it. See Predicted Hierarchy for Create and Delete.

Null other in physics callbacks

Even with the scene set up correctly, other can be null on an Exit event: when the other object was deleted while touching, the Exit still fires but its GameObject is already gone. Null-check other and key your bookkeeping by otherId. See Collision and trigger events.