# 3. Persistence

## Physics & Persistence

This scene will show you how easy it is to set up Networking in your Unity project and sync GameObject transforms and Physics objects across the network whilst keeping them persistent. As long as the server is running you can disconnect and re-connect, your world will persist.

In this example a right click will spawn local physics based objects that all other players will see. These physics objects will interact with each other and the physics for every object will be simulated locally on its authority. Clicking one of these objects will either take or relieve authority of the local client over the clicked object. You can disconnect and re-connect and the persistent entities will all remain.

![](https://3580906557-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUrQ8GJkdHxImzMEWuxaz%2Fuploads%2Fgit-blob-903a7970c035e7be1fcd8449e0f5968a56a2340d%2Fezgif.com-gif-maker%20\(16\).gif?alt=media)

The controls at the top right of the screen allow the spawning of a unique object that works very similarly to the other physics based objects. This bigger cube is set to `No Duplicates` in it's `Uniqueness` property which means the server will only allow one instance of this object to exist at a time. It also has it's `Lifetime` setting set to `Session Based` this will cause the object to be deleted when it's owner disconnects. Just like with the physics based objects, a player can claim authority over this object by clicking it. If that player would then disconnect all clients will have the unique object deleted.

![](https://3580906557-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUrQ8GJkdHxImzMEWuxaz%2Fuploads%2Fgit-blob-5b35744075bd351bce5becb43da3fa8fb8f47786%2FGIF%2002-12-2021%205-02-50.gif?alt=media)

### General Set Up

In the *Hierarchy* of the Scene you can see three core Prefabs:

`Core Scene Setup` and `Coherence Setup` are present in all scenes and described in detail on [Start Tutorial](https://docs.coherence.io/0.8/tutorial-project/network-playground) page.

`Coherence Entity` is not present in this scene.

`Input Manager` in the `Core Scene Setup` prefab is set up to spawn a `Sample Entity Physics Persistent` where a click is performed.

`Coherence Connection Events` handles overall Scene connectivity. In this scene we use it to cleanup objects the client has authority over when disconnecting.

### In This Scene...

The `Physics Entity Spawner` component is a simple script to instantiate a `Coherence Entity Physics Persistent` prefab with a coherenceSync component that replicates the transform and position. The component also changes the material based on if it is locally simulated or synced over the network.

The `Coherence Entity Physics` variants have an `Entity Lifetime Type` set to `Persistent`. This means it will remain in the world as long as the replication server is running, even if all clients disconnect. It also has `Authority Transfer Style` set to `Stealing` which means the entity can be "stolen" and simulated on a client requesting authority.

This is done via the `Input Manager` in the `Core Scene Setup` prefab. When the object is left-clicked, it sends the message "Adopt" to the GameObject on the specific Layer Mask "Physics Entities". The component called `Coherence Handler` on `Coherence Entity Physics` objects handle the Adopt call and requests the authority change via the coherenceSync component.

`Coherence Handler` is a basic layer for handling Commands and Events, both sending and receiving. You can create your own or reuse and extend this for your project.

![](https://3580906557-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUrQ8GJkdHxImzMEWuxaz%2Fuploads%2Fgit-blob-82c0d7d2a879910f9c6b2c374b28ce6521cb39c3%2Fplayground_scene3-4-.png?alt=media)

### Build and Try

You can build this Scene via the Build Settings. Run the local Replication Server through the *Window -> Coherence -> Settings* window and see how it works. You can try running multiple clients rather than just two and see replicating for each.
