1

For my PlayerController script, I changed inheriting from MonoBehaviour to NetworkBehaviour

public class PlayerController : MonoBehaviour
{

From this, to this:

public class PlayerController : NetworkBehaviour
{

Even though my player controller object does have a NetworkObject component.

Its throwing an error saying that it doesn't have one.

NullReferenceException: Could not get NetworkObject for the NetworkBehaviour. Are you missing a NetworkObject component?
MLAPI.NetworkBehaviour.get_NetworkObject () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkBehaviour.cs:282)
MLAPI.NetworkObject.get_ChildNetworkBehaviours () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkObject.cs:515)
MLAPI.NetworkObject.ResetNetworkStartInvoked () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkObject.cs:472)
MLAPI.Spawning.NetworkSpawnManager.SpawnNetworkObjectLocally (MLAPI.NetworkObject networkObject, System.UInt64 networkId, System.Boolean sceneObject, System.Boolean playerObject, System.Nullable`1[T] ownerClientId, System.IO.Stream dataStream, System.Boolean readPayload, System.Int32 payloadLength, System.Boolean readNetworkVariable, System.Boolean destroyWithScene) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Spawning/NetworkSpawnManager.cs:395)
MLAPI.NetworkManager.StartHost (System.Nullable`1[T] position, System.Nullable`1[T] rotation, System.Nullable`1[T] createPlayerObject, System.Nullable`1[T] prefabHash, System.IO.Stream payloadStream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:556)
NetworkManagerEditor.OnInspectorGUI () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Editor/NetworkManagerEditor.cs:371)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <31768fe99cfe4466aa4a401169fb2ce5>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Its throwing this error as soon as I click on "Start Host" on the Network Manager script/object.

How can I fix this issue?

John
  • 5,942
  • 3
  • 42
  • 79

1 Answers1

0

For some reason. There was a race condition that made GetComponentInParent return null. So I edited NetworkBehaviour.cs to use GetComponent instead.

    /// <summary>
    /// Gets the NetworkObject that owns this NetworkBehaviour instance
    /// </summary>
    public NetworkObject NetworkObject
    {
        get
        {
            if (m_NetworkObject == null)
            {
                //m_NetworkObject = GetComponentInParent<NetworkObject>();

                m_NetworkObject = GetComponent<NetworkObject>();
            }

            if (m_NetworkObject == null)
            {
                throw new NullReferenceException($"Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?");
            }

            return m_NetworkObject;
        }
    }
John
  • 5,942
  • 3
  • 42
  • 79