PurrNet
  • 🐈Introduction
    • ‼️Unique to PurrNet
    • 💲Pricing
    • 💻Compatibility
    • 📚Addon library
    • 🗺️Roadmap
    • 🏎️Performance
      • RPC Benchmarks
      • Network Transform benchmarks
    • 🥰Support PurrNet
  • 🏁Getting Started
    • Installation
    • Minimal Setup
    • Converting to PurrNet
      • Converting from Mirror
      • Converting from FishNet
  • 📚Simple Guides
    • Easy Multiplayer Physics (input Sync)
  • 🖥️Systems and modules
    • Network Manager
      • Network Rules
      • Network Prefabs
      • Network Visibility
        • Distance condition
      • Authentication
    • Transports
      • Composite Transport
      • UDP Transport
      • Web Transport
      • Local Transport
      • Steam Transport
      • Purr Transport
      • Simulating latency
    • Network Identity
      • Ownership
      • Spawning & Despawning
      • NetworkBehaviour
      • Don't destroy on load
    • Network Modules
      • Common Pitfalls
      • Sync Types
        • SyncVar
        • SyncList
        • SyncArray
        • SyncQueue
        • SyncDictionary
        • SyncEvent
        • SyncHashset
        • SyncTimer
        • SyncInput
    • Remote Procedure Calls
      • Generic RPC
      • Static RPC
      • Awaitable RPC
      • Direct Local Execution of RPCs
    • Broadcasts
    • Scene Management
    • Client Side Prediction
      • Overview
      • Predicted Identities
      • Predicted Hierarchy
      • Best Practices
      • Input Handling
      • State Handling
    • Instance Handler
    • Collider Rollback
    • BitPacker
      • Networking custom classes, structs & types
  • Plug n' play components
    • Network Transform
    • Network Animator
    • Network Ownership Toggle
    • Network Reflection
    • Network State Machine
  • 🎮Full game guides
    • 🔫First Person Shooter
    • 🪓Survival Game
  • ✊Community Guides
    • Chat system with broadcasts
  • 🧩Addons
    • Lobby System
  • 🤓Terminology
    • Early Access
    • Channels
    • Client Auth/Everyone (Unsafe)
    • Host
    • Server Auth (Safe)
    • PlayerID
  • 💡Integrations
    • Dissonance
    • Cozy Weather
Powered by GitBook
On this page
  • Common Pitfalls
  • 1. Modules Must Be Fields or Properties
  • 2. Editor Serialization
  • 3. Initialization: Use OnInitializeModules()
  • 4. Do Not Replace Modules After Spawn
  • 5. Nesting Modules
  • 6. No Lists of Modules
  • 7. Module Events
  1. Systems and modules
  2. Network Modules

Common Pitfalls

Common Pitfalls

1. Modules Must Be Fields or Properties

Network modules must be declared as a field or property on a NetworkIdentity. Do not create them dynamically or store them in collections.

public class MyNetworkObject : NetworkIdentity
{
    public SyncVar<int> health; // ✅ Correct
    private SyncList<string> names; // ✅ Correct

    // ❌ Incorrect: Do not use lists of modules
    public List<SyncVar<int>> invalidList; 
}

2. Editor Serialization

Modules can be serialized and shown in the Unity Editor. Just mark them with [SerializeField] or make them public.

[SerializeField]
private SyncVar<int> health; // Will show up in the Inspector

public SyncList<string> names; // Also visible if public

3. Initialization: Use OnInitializeModules()

You can create, override, or initialize modules in the protected virtual void OnInitializeModules() callback. This runs before networking sends data or assigns IDs.

protected override void OnInitializeModules()
{
    health = new SyncVar<int>(100);
    // Custom initialization here
}

4. Do Not Replace Modules After Spawn

Once spawned, never assign a new module instance to a field/property. Doing so will break networking or cause undefined behavior. Modules are meant to be constant after spawn.

// ❌ Don't do this after spawn:
health = new SyncVar<int>(200); // Will break networking!

5. Nesting Modules

You can nest modules—meaning a network module can use other modules like SyncVar as fields or properties.

public class MyModule : NetworkModule
{
    public SyncVar<int> score; // ✅ Allowed
}

Do not create circular dependencies. If Module A uses Module B, and Module B uses Module A, it will break. Nesting must form a tree, not a loop.

// ❌ Not allowed: Circular reference
public class ModuleA : NetworkModule
{
    public ModuleB b;
}

public class ModuleB : NetworkModule
{
    public ModuleA a; // This creates a cycle and will cause compiler error
}

6. No Lists of Modules

You cannot create a list or array of modules. Modules must be declared as fields or properties at compile time.

// ❌ Not allowed:
public List<SyncVar<int>> moduleList;

// ✅ Allowed:
public SyncVar<int> health;
public SyncVar<int> mana;

7. Module Events

Modules have similar events to NetworkIdentity, such as:

  • OnSpawned

  • OnOwnerChanged

  • OnDespawned

You can use these for custom logic inside your modules.

public class MyModule : NetworkModule
{
    protected override void OnSpawned() { /* ... */ }
    protected override void OnOwnerChanged(..) { /* ... */ }
    protected override void OnDespawned() { /* ... */ }
}
PreviousNetwork ModulesNextSync Types

Last updated 2 days ago

🖥️