Search Unity

"Could not get NetworkObject for the NetworkBehaviour"

Discussion in 'Netcode for GameObjects' started by RyanGarber, Jul 18, 2021.

  1. RyanGarber

    RyanGarber

    Joined:
    May 20, 2021
    Posts:
    11
    I apologize if I come off aggro. I have just spent 20+ hours converting my game to the MLAPI, knowing well that it's experimental.

    The issue: I created a second prefab. It contains a NetworkObject, and a NetworkBehaviour. I spawned it with
    Instantiate({ReferenceToPrefab});
    like I did with my first prefab. I then called
    GetComponent<NetworkObject>().Spawn()
    like before. Yet it says "Could not get NetworkObject for this NetworkBehaviour." Given this killed all of my productivity, I've spent the last hour debugging every bit I can. And the more I do, the more I realize that my project might be forever stuck at this point, while the developers say stuff like "I don't even like making games" while working for a game engine, instead of actually testing or giving examples. If anybody has any tips, or knows something I don't, I could use the motivation.

    The proof: image1.jpeg
    image0.jpeg
     
    Last edited: Jul 18, 2021
  2. RyanGarber

    RyanGarber

    Joined:
    May 20, 2021
    Posts:
    11
    And for the record, I definitely have it set up correctly.
     

    Attached Files:

  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Hi Ryan, sorry about that. Your setup looks correct from what I can tell, we use similar code in our samples to spawn network objects at runtime so I'm not sure what's wrong. Probably you are encountering a rare bug in MLAPI which is not known to us yet.

    I'll try to help you get unstuck to my best extend. An operation like spawning an object is something which should definitely work. To get started could you confirm the following:
    - The code above is executed on a host/server and not a client
    - You are using the 0.1.0 version and followed the documentation for installing it.
    - At the time when you are running the code above MLAPI is already started. E.g. you are not calling the above in a MonoBehaviour Awake/Start or something like that.

    If you could provide a full stack trace of the exception that could also help us figure out what's wrong.

    As for your comment about us saying "I don't even like making games". I cannot speak for the entire team but I and many others that I know in our team love making games and especially multiplayer games and we are working hard on improving MLAPI and making it more stable.

    Finally I just wanted to say I really understand why you are frustrated, I'd probably be frustrated as well if I were in your situation. So I hope that I'll be able to help you out here.
     
  4. Bernardolv

    Bernardolv

    Joined:
    Jan 26, 2018
    Posts:
    7
    I am also getting this error, it only happens when I run the server/host outside the unity editor.

    I have tried having the barebones minimum.

    As soon as a client joins and the player prefab is spawned it spams me with these errors.

    my player script on player prefab is literally empty just inherits from NetworkBehaviour for test purposes. And the prefab also has a NetworkObject as well as a sprite renderer.

    I insist I believe it is important to know that this is only happening when the server/host is NOT in the unity editor.
     
  5. Bernardolv

    Bernardolv

    Joined:
    Jan 26, 2018
    Posts:
    7
    [Netcode] Could not get NetworkObject for the NetworkBehaviour. Are you missing a NetworkObject component?

    is the full error
     
  6. Skanou_Dev

    Skanou_Dev

    Joined:
    Jun 22, 2017
    Posts:
    7
    Hi,

    I've got the same warning constantly with Netcode for GameObjects version 1.0.0-pre.5, when log level is developper. When I tracked down the warning, it seems that it comes from this code in the NetworkBehaviour:

    Code (CSharp):
    1. public NetworkObject NetworkObject
    2.         {
    3.             get
    4.             {
    5.                 if (m_NetworkObject == null)
    6.                 {
    7.                     m_NetworkObject = GetComponentInParent<NetworkObject>();
    8.                 }
    9.  
    10.                 if (m_NetworkObject == null || NetworkManager.Singleton == null ||
    11.                     (NetworkManager.Singleton != null && !NetworkManager.Singleton.ShutdownInProgress))
    12.                 {
    13.                     if (NetworkLog.CurrentLogLevel < LogLevel.Normal)
    14.                     {
    15.                         NetworkLog.LogWarning($"Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?");
    16.                     }
    17.                 }
    18.  
    19.                 return m_NetworkObject;
    20.             }
    21.         }
    The problem seems to be
    (NetworkManager.Singleton != null && !NetworkManager.Singleton.ShutdownInProgress)

    I don't see why ShutdownInProgress shoud be false. This condition is always true when the server is running.
     
    gyx920820 likes this.
  7. gyx920820

    gyx920820

    Joined:
    Oct 8, 2015
    Posts:
    35

    definitely bug
     
  8. Skanou_Dev

    Skanou_Dev

    Joined:
    Jun 22, 2017
    Posts:
    7
    Seems fixed in Netcode for GameObjects version 1.0.0-pre.6.