<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mindattic.com/wiki/docs/index.php?action=history&amp;feed=atom&amp;title=Unity.Multiplayer%2Farticles%2Fobjects</id>
	<title>Unity.Multiplayer/articles/objects - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mindattic.com/wiki/docs/index.php?action=history&amp;feed=atom&amp;title=Unity.Multiplayer%2Farticles%2Fobjects"/>
	<link rel="alternate" type="text/html" href="https://mindattic.com/wiki/docs/index.php?title=Unity.Multiplayer/articles/objects&amp;action=history"/>
	<updated>2026-12-05T05:09:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>https://mindattic.com/wiki/docs/index.php?title=Unity.Multiplayer/articles/objects&amp;diff=932&amp;oldid=prev</id>
		<title>Docs Sync: md2wiki: sync from MindAttic.Docs</title>
		<link rel="alternate" type="text/html" href="https://mindattic.com/wiki/docs/index.php?title=Unity.Multiplayer/articles/objects&amp;diff=932&amp;oldid=prev"/>
		<updated>2026-11-29T20:37:08Z</updated>

		<summary type="html">&lt;p&gt;md2wiki: sync from MindAttic.Docs&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Networked objects by key =&lt;br /&gt;
&lt;br /&gt;
A game usually knows its objects by its own names: a level&amp;#039;s block &amp;lt;code&amp;gt;b12&amp;lt;/code&amp;gt;, the door &amp;lt;code&amp;gt;door:A&amp;lt;/code&amp;gt;, the scoreboard. On a client, though, a spawned object arrives as just another &amp;lt;code&amp;gt;NetworkObject&amp;lt;/code&amp;gt;, and matching it back to the game&amp;#039;s name by spawn order or by network object id breaks as soon as the order changes, an object is replaced, or a player joins late.&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;strong&amp;gt;network key&amp;lt;/strong&amp;gt; fixes that. The server gives each object a key when it spawns it; every peer looks the object up by that key in the session&amp;#039;s registry.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;md-code lang-csharp&amp;quot;&amp;gt;// Server, building a level: each block&amp;#039;s body is keyed by the block&amp;#039;s id.&lt;br /&gt;
foreach (Block block in level.Blocks)&lt;br /&gt;
{&lt;br /&gt;
    GameObject body = session.Objects.Spawn(bodyPrefab, block.Id, block.Position);&lt;br /&gt;
    // ... configure the server&amp;#039;s copy as usual&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Any peer, drawing the level: find the replicated body for each block.&lt;br /&gt;
foreach (Block block in level.Blocks)&lt;br /&gt;
{&lt;br /&gt;
    if (session.Objects.TryGet(block.Id, out Transform body))&lt;br /&gt;
        Draw(block, body.position);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Set up ==&lt;br /&gt;
&lt;br /&gt;
# Add a &amp;lt;strong&amp;gt;&amp;lt;code&amp;gt;NetKey&amp;lt;/code&amp;gt;&amp;lt;/strong&amp;gt; component to the root of the network prefab, beside its &amp;lt;code&amp;gt;NetworkObject&amp;lt;/code&amp;gt;. Register the prefab as usual (a &amp;lt;code&amp;gt;NetPrefabList&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;session.Prefabs.Register&amp;lt;/code&amp;gt; on every peer before the session starts).&lt;br /&gt;
# On the server, spawn instances with &amp;lt;strong&amp;gt;&amp;lt;code&amp;gt;session.Objects.Spawn(prefab, key, position, rotation, parent, ownerClientId)&amp;lt;/code&amp;gt;&amp;lt;/strong&amp;gt;. It refuses a bad or already-used key before anything is created, sets the key, and spawns through this session.&lt;br /&gt;
# On any peer, use &amp;lt;strong&amp;gt;&amp;lt;code&amp;gt;session.Objects&amp;lt;/code&amp;gt;&amp;lt;/strong&amp;gt; (or &amp;lt;code&amp;gt;NetRegistry.Current&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;NetBootstrap.Instance&amp;lt;/code&amp;gt;&amp;#039;s session).&lt;br /&gt;
&lt;br /&gt;
The key travels inside the object&amp;#039;s spawn, so a client knows it before any of the object&amp;#039;s &amp;lt;code&amp;gt;OnSpawned&amp;lt;/code&amp;gt; runs, and a late joiner gets every existing object&amp;#039;s key the same way. A prefab whose &amp;lt;code&amp;gt;NetKey&amp;lt;/code&amp;gt; has a key set in the Inspector spawns with that key (handy for a one-of-a-kind object like a scoreboard).&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;NetRegistry&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;session.Objects&amp;lt;/code&amp;gt;) ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Member&lt;br /&gt;
! What it does&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;TryGet&amp;amp;lt;T&amp;gt;(key, out T)&amp;lt;/code&amp;gt;&lt;br /&gt;
| The spawned object holding &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt;, as its &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; component: the &amp;lt;code&amp;gt;NetKey&amp;lt;/code&amp;gt;, any component on the object or its children (&amp;lt;code&amp;gt;Transform&amp;lt;/code&amp;gt;, your own &amp;lt;code&amp;gt;NetEntity&amp;lt;/code&amp;gt;, ...). False if no object holds the key on this peer.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;TryGet(key, out GameObject)&amp;lt;/code&amp;gt;&lt;br /&gt;
| The same, as the &amp;lt;code&amp;gt;GameObject&amp;lt;/code&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Get&amp;amp;lt;T&amp;gt;(key)&amp;lt;/code&amp;gt;&lt;br /&gt;
| The same, or a &amp;lt;code&amp;gt;KeyNotFoundException&amp;lt;/code&amp;gt; naming the key (an &amp;lt;code&amp;gt;InvalidOperationException&amp;lt;/code&amp;gt; if the object has no &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;).&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Contains(key)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Count&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Keys&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;All&amp;lt;/code&amp;gt;&lt;br /&gt;
| What is spawned on this peer, by key.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Spawned&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Despawned&amp;lt;/code&amp;gt;&lt;br /&gt;
| A keyed object spawned on, or is despawning from, this peer (&amp;lt;code&amp;gt;Action&amp;amp;lt;NetKey&amp;gt;&amp;lt;/code&amp;gt;; &amp;lt;code&amp;gt;.Key&amp;lt;/code&amp;gt; is its key). A late joiner gets &amp;lt;code&amp;gt;Spawned&amp;lt;/code&amp;gt; for each object already in the game as it arrives.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Spawn(prefab, key, position, rotation, parent, ownerClientId)&amp;lt;/code&amp;gt;&lt;br /&gt;
| Server: spawns an instance of &amp;lt;code&amp;gt;prefab&amp;lt;/code&amp;gt; holding &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Despawn(key)&amp;lt;/code&amp;gt;&lt;br /&gt;
| Server: despawns and destroys the object holding &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt;; false if none does.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;NetRegistry.Current&amp;lt;/code&amp;gt;&lt;br /&gt;
| The registry of &amp;lt;code&amp;gt;NetBootstrap.Instance&amp;lt;/code&amp;gt;&amp;#039;s session.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Events are raised on Unity&amp;#039;s main thread, after every component of the object has run its &amp;lt;code&amp;gt;OnSpawned&amp;lt;/code&amp;gt; (and before any runs &amp;lt;code&amp;gt;OnDespawned&amp;lt;/code&amp;gt;). A handler that throws is logged and does not stop the others. The registry empties when its session starts and ends.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;NetKey&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Member&lt;br /&gt;
! What it does&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Key&amp;lt;/code&amp;gt;&lt;br /&gt;
| The key. Set it before the object spawns (&amp;lt;code&amp;gt;Spawn&amp;lt;/code&amp;gt; does); setting it while spawned throws, because a key is stable for the object&amp;#039;s whole life.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;IsRegistered&amp;lt;/code&amp;gt;&lt;br /&gt;
| True while the registry hands this object out for its key (false for a refused duplicate).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Rules for keys ==&lt;br /&gt;
&lt;br /&gt;
* A key is a non-empty string of at most &amp;lt;code&amp;gt;NetKeys.MaxLength&amp;lt;/code&amp;gt; (128) characters, with no control characters and no leading or trailing whitespace (&amp;lt;code&amp;gt;NetKeys.Validate&amp;lt;/code&amp;gt; says what is wrong with one). Keys are compared ordinally: &amp;lt;code&amp;gt;B1&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b1&amp;lt;/code&amp;gt; are different keys.&lt;br /&gt;
* &amp;lt;strong&amp;gt;One live object per key.&amp;lt;/strong&amp;gt; &amp;lt;code&amp;gt;Spawn&amp;lt;/code&amp;gt; throws an &amp;lt;code&amp;gt;ArgumentException&amp;lt;/code&amp;gt; for a key a spawned object already holds, naming the key and that object. To reuse a key, despawn the old object first: &amp;lt;code&amp;gt;Despawn(key)&amp;lt;/code&amp;gt; frees it at once, so a level reset can despawn its old objects and spawn the new ones with the same keys in the same frame. (Unity&amp;#039;s &amp;lt;code&amp;gt;Object.Destroy&amp;lt;/code&amp;gt; despawns only at the end of the frame; despawn first if you rebuild in the same frame.)&lt;br /&gt;
* An object spawned some other way (&amp;lt;code&amp;gt;NetworkObject.Spawn&amp;lt;/code&amp;gt;, the test kit&amp;#039;s &amp;lt;code&amp;gt;Spawn&amp;lt;/code&amp;gt;) with no key or a key in use is reported as an error on every peer and left out of the registry; the object that had the key keeps it.&lt;br /&gt;
&lt;br /&gt;
== Where it is tested ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;dotnet test tests/Core.Tests&amp;lt;/code&amp;gt; covers the engine-free rules (&amp;lt;code&amp;gt;NetKeysTests&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;NetKeyTableTests&amp;lt;/code&amp;gt;: validation, one holder per key, a removal only by the holder). &amp;lt;code&amp;gt;tests/run-playmode.ps1&amp;lt;/code&amp;gt; runs &amp;lt;code&amp;gt;NetRegistryTests&amp;lt;/code&amp;gt; with a Host and with a dedicated Server, each with two clients over real Unity Transport: every client finds each object by key (and its components see the key in their own &amp;lt;code&amp;gt;OnSpawned&amp;lt;/code&amp;gt;), a late joiner finds objects spawned before it came, objects spawned out of order and replaced still map to the right key, a despawn is heard everywhere and frees the key for an object spawned in the same frame, and a duplicate key is refused (by &amp;lt;code&amp;gt;Spawn&amp;lt;/code&amp;gt; before anything spawns, and reported on every peer for an object spawned another way).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;md2wiki-crumb&amp;quot;&amp;gt;Part of [[Unity.Multiplayer]].&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;md2wiki-source&amp;quot;&amp;gt;From [https://github.com/mindattic/Unity.Multiplayer/blob/main/docs/articles/objects.md Unity.Multiplayer/docs/articles/objects.md] via MindAttic.Docs; synced automatically, so edit the repo, not this page.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Unity.Multiplayer]]&lt;/div&gt;</summary>
		<author><name>Docs Sync</name></author>
	</entry>
</feed>