Skip to content

API: Spawning and Entities

Lyraedan edited this page Sep 24, 2026 · 6 revisions

SpawnUtilsAPI lets the host create entities that are automatically assigned a NetworkIdentity and replicated to all clients. NetworkIdentityRegistryAPI resolves those entities later by their network id (NetId).

Namespaces:

  • ONI_Together_API.Misc → SpawnUtilsAPI
  • ONI_Together_API.Networking → NetworkIdentityRegistryAPI
  • Shared.Helpers → NetIdentityHelper

Spawning networked entities

Only the host may spawn. Both overloads return null when ONI Together isn't loaded, the local player isn't the host, or the input is invalid.

using ONI_Together_API.Misc;
using UnityEngine;

// Spawn a prefab by GameObject.
GameObject spawned = SpawnUtilsAPI.KNetInstantiate(somePrefab, position);

// Spawn an element resource (e.g. ore) preserving mass, temperature and disease.
GameObject ore = SpawnUtilsAPI.KNetInstantiate(
    elementHash: (int)SimHashes.IronOre,
    position:    position,
    mass:        100f,
    temperature: 293.15f,
    diseaseIdx:  0,
    diseaseCount: 0);
Overload Parameters
KNetInstantiate(GameObject prefab, Vector3 position, bool isActive = true) Prefab + world position
KNetInstantiate(int elementHash, Vector3 position, float mass, float temperature, byte diseaseIdx, int diseaseCount) Element resource with simulation data

Looking up networked entities

Every networked entity is registered under an int network id (NetId) assigned by its NetworkIdentity. NetworkIdentityRegistryAPI resolves those ids on any peer.

Method Returns Description
TryGet(int netId, out GameObject gameObject) bool GameObject for a NetId
TryGetComponent<T>(int netId, out T component) bool Component of type T on that GameObject

Both return false (and a null/default out value) if the entity isn't found or the mod isn't loaded.

using ONI_Together_API.Networking;
using UnityEngine;

if (NetworkIdentityRegistryAPI.TryGet(netId, out GameObject entity))
{
    // use entity
}

if (NetworkIdentityRegistryAPI.TryGetComponent<PrimaryElement>(netId, out var primaryElement))
{
    // use primaryElement
}

Getting and assigning a NetId

When creating objects yourself, ensure a NetworkIdentity exists. NetIdentityHelper is wired up when the API is bridged and gives you fine-grained control over that identity. All methods return 0 when ONI Together isn't loaded or no identity is present.

Method Description
GetNetId(GameObject go) Reads the existing NetId without creating or registering a NetworkIdentity. Returns 0 if none exists.
AddNetId(GameObject go) Adds a NetworkIdentity if missing, registers it if it has no NetId, and returns the resolved NetId.
AddOrGetNetId(GameObject go, int preferredId = 0) Same as AddNetId, but forces preferredId when it is non-zero.
OverrideNetId(GameObject go, int netId) If a NetworkIdentity exists, override its NetId to netId.
using Shared.Helpers;

// Read-only lookup — does not create an identity.
int existing = NetIdentityHelper.GetNetId(gameObject);

// Assign a NetId, creating and registering an identity if needed.
int netId = NetIdentityHelper.AddNetId(gameObject);

// Add a new NetworkIdentity, or fetch the existing one.
int preferred = NetIdentityHelper.AddOrGetNetId(gameObject, preferredId);

// Override the existing id on the game object with a new one.
int forced = NetIdentityHelper.OverrideNetId(gameObject, id);

For most cases prefer SpawnUtilsAPI.KNetInstantiate, which handles identity assignment and replication for you.

Clone this wiki locally