Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Setup NetworkBehavior in another scene.

Discussion in 'Netcode for GameObjects' started by harsh_agarwal_gamestacy, Jan 24, 2023.

  1. harsh_agarwal_gamestacy

    harsh_agarwal_gamestacy

    Joined:
    Apr 18, 2022
    Posts:
    5
    I have two scenes. One is responsible for matchmaking (so it has Lobby and Relay components) and the next scene has the actual game (so it has a NetworkBehavior for RPCs).

    Now my NetworkBehavior doesn't work which I'm guessing is because the NetworkObject component isn't set on account of it being in another scene and the NetworkManager + Unity Transport components being in another scene.

    How do I make it so that this works?
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hey @harsh_agarwal_gamestacy , I'll need more info to help you:
    1. Are you using the default scene management or a custom one?
    2. What do you mean by "my NetworkBehavior doesn't work" ? What's the symptom?
    3. Can you post the code that shows how are you trying to interact with your NetworkBehaviour?
    4. Who has authority over that object?
     
  3. harsh_agarwal_gamestacy

    harsh_agarwal_gamestacy

    Joined:
    Apr 18, 2022
    Posts:
    5
    Hey @RikuTheFuffs-U , here's the necessary info:
    1. Yes, I'm using the default scene management.
    2. On the server, the NetworkBehavior object is set to empty with the "Spawn" button being enabled during runtime. If I click on the Spawn button, it fills in the details on the object and on the clients it gives an error. If I try to pass any RPCs between the servers/clients, all the receiving peers show that the message was delayed by a second in the debug logs and don't do anything.
    3. I'm just calling the various RPCs I've defined in the NetworkBehavior.
    4. I'm not setting the authority explicitly. So I think each device/game instance has its own copy of the object and has authority over that? I didn't go too deep into trying to understand this system as I got my game working earlier without setting authorities/spawning multiple objects. Let me know if you think that's the wrong approach. Since I'm making a board game (like chess, but there can be 4 players and each player can have dozens of pieces), I didn't want to create a NetworkObject for every piece and as per my understanding, I can just use it to pass messages between peers.

    Thanks!
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Thanks for the answers, @harsh_agarwal_gamestacy !

    I'm not sure I understood, what do you mean by "empty"? What NetworkObject are you referring to? Can you please clarify?

    From which script/method are you calling them? Without code, it's hard to help you
     
  5. harsh_agarwal_gamestacy

    harsh_agarwal_gamestacy

    Joined:
    Apr 18, 2022
    Posts:
    5
    Hi @RikuTheFuffs-U

    By empty, I mean none of the properties of the component are visible in the inspector. I'm talking about the NetworkObject component that gets auto-added to any object that has a NetworkBehavior script.

    So for example I have:
    Code (CSharp):
    1. class RPCClass: NetworkBehavior
    2. {
    3.      [ServerRPC]
    4.      public void DoSomethingClientRPC(){}
    5. }
    I'll call it like this:
    Code (CSharp):
    1. class SomeScript: MonoBehavior
    2. {
    3.      public RPCClass refToClass;
    4.      public void OnSomeEvent()
    5.      {
    6.      refToClass.DoSomethingClientRPC();
    7.      }
    8. }
    Both the scripts are in the same scene, but on different objects.
     
  6. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    If your method is a [ServerRPC] its name has to end with ServerRPC. It's not working because you called it DoSomethingClientRPC. The correct name would be DoSomethingServerRPC.

    Does this fix your problem?
     
  7. harsh_agarwal_gamestacy

    harsh_agarwal_gamestacy

    Joined:
    Apr 18, 2022
    Posts:
    5
    @RikuTheFuffs-U hey, sorry for the confusion. It was a mistake here, I am actually using the right method names in my project. So in the project, it is actually DoSomethingServerRPC
     
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Ok, has the object with RPCClass being spawned over the network (not just in the scene) when you try to call that method? Do you have any logs in the console?