Search Unity

Resolved [Netcode] Behaviour index was out of bounds. Did you mess up the order of your NetworkBehaviours?

Discussion in 'Unity Transport' started by Seforius, Aug 30, 2022.

  1. Seforius

    Seforius

    Joined:
    Jun 25, 2022
    Posts:
    10
    So I've been following some tutorials online and have been trying to create a multiplayer 3D game, and so far, all I want to do is for two clients to be able to connect to a server, spawn their players (spheres), and move their spheres around and into each other, causing collisions.

    I have created a player movement script to handle input - just simple WASD and space-jump movement.

    To handle the networking, I followed Tarodev's Netcode video to set everything up.

    Here is where the problem begins. Using ParrelSync, I run the game on one editor and a clone on another. I then use a GameObject with the NetworkManager script to start a host on one editor, and a client on the other. Upon connection, the client and host both spawn players, but then the problem begins. As the client's player starts falling down, the host's editor starts experiencing errors and warnings. As mentioned in the title, here they are:

    [Netcode] Behaviour index was out of bounds. Did you mess up the order of your NetworkBehaviours?

    [Netcode] Network variable delta message received for a non-existent behaviour. NetworkObjectId: 2, NetworkBehaviourIndex: 2

    I did a bit of digging and found that the error was being thrown by a function in the NetworkObject script, but I couldn't really understand why or how to fix it, hence this post.

    Errors are only thrown on the client when the host moves its player, and errors are only thrown on the host when the client moves its player. The transforms of the players are not being transmitted between host and client at all, despite the player prefab having a Client Network Transform script, and a NetworkObject script.

    Here is the link to the code if you want to reproduce the error or understand it better: https://github.com/XR101/Sumo101

    Any help would be appreciated as this error is driving me insane.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    I took a look at the code, I wasn't able to get it running as the scene was empty, but that might have been the way I copied over to a new project.

    The issue is likely where you use this line:
    Code (CSharp):
    1.  if(!IsOwner) {Destroy(this);}
    From what I can see Tarodev is using it to remove the PlayerController component from other player objects leaving the one owned by the client. If you use it to destroy a component that the server needs to communicate with it will result in the above errors. Try commenting them out and see if it makes a difference.
     
  3. edin97

    edin97

    Joined:
    Aug 9, 2021
    Posts:
    58
    For me : the reason I was having this issue was because I use an "Object Pooler" to spawn NetworkObjects at the beggining and set them incative until need. The crazy part is, I NEVER touched this script and the bug came from there.
    I removed junk code from ANOTHER script I called "Spell" that is attached to the NetworkObjects the Object Pooler was spawning. I was doing this in the object pooler :
    Code (CSharp):
    1.     GameObject obj = Instantiate(pool.Prefab);
    2.     obj.SetActive(false);
    3.     obj.GetComponent<NetworkObject>().Spawn();
    4.  
    Again, this NEVER changed and worked.

    This is what I did
    after this bug came up on my "Spell" script, which I figured meant the NetworkObject was not initialized correctly for some reason. Even though it always worked ... :
    Code (CSharp):
    1.     GameObject obj = Instantiate(pool.Prefab);
    2.     obj.GetComponent<NetworkObject>().Spawn();
    3.     obj.SetActive(false);
    I just swapped line 2 and line 3 ... I guess Spawn needs to come before any changes is made to the NetworkObject but idk...
    Hopefully this helps someone, but this is crazy.
     
  4. detector_tian

    detector_tian

    Joined:
    Jul 3, 2022
    Posts:
    2
    As cerestorm mentioned, the issue is likely in this line:
    Code (CSharp):
    1. if (!IsOwner) Destory(this);
    The script may not be deleted dynamically due to netcode internal requirements.So I changed the code and it works.
    Code (CSharp):
    1. if (!IsOwner) enabled = false;
     
  5. firebird721

    firebird721

    Joined:
    Jun 8, 2022
    Posts:
    101


    thank you all very much i was so confused by this problem - i never could have solved it by myself - thx
     
    detector_tian likes this.