Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved ServerRpc not being called

Discussion in 'Netcode for GameObjects' started by lazy_clap, Nov 1, 2023.

  1. lazy_clap

    lazy_clap

    Joined:
    Jul 9, 2021
    Posts:
    7
    Hi i have a program where I'm trying to spawn a network object.
    My issue is that my server rpc function doesn't seem to be being called.

    Here are my scripts:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class NetworkSpawner : NetworkBehaviour
    7. {
    8.     public static NetworkSpawner Instance { get; private set; }
    9.  
    10.     [SerializeField] ObjectListSO objectListSO;
    11.          
    12.     private void Awake()
    13.     {
    14.         Instance = this;  
    15.     }
    16.  
    17.     public void SpawnObject(ObjectSO objectSO)
    18.     {
    19.         SpawnObjectServerRpc(GetObjectSOIndex(objectSO));//issue is here, correct index is being passed but the ServerRPC itself is not being run
    20.  
    21.         print("Spawn Object run");
    22.        
    23.  
    24.     }
    25.  
    26.     [ServerRpc(RequireOwnership = false)]
    27.     private void SpawnObjectServerRpc(int objectSOIndex, ServerRpcParams serverRpcParams = default)
    28.     {
    29.         print("run!!!!!!!!!!");
    30.         ObjectSO objectSO = GetObjectSOFromIndex(objectSOIndex);
    31.  
    32.         Transform objectTransform = Instantiate(objectSO.prefab);
    33.         NetworkObject networkObject = objectTransform.GetComponent<NetworkObject>();
    34.         networkObject.Spawn(true);
    35.  
    36.     }
    37.  
    38.     private int GetObjectSOIndex(ObjectSO objectSO)
    39.     {
    40.         return objectListSO.objectList.IndexOf(objectSO);//working returns 0
    41.     }
    42.  
    43.     private ObjectSO GetObjectSOFromIndex(int objectIndex)
    44.     {
    45.         return objectListSO.objectList[objectIndex];//working returns BallSO when given index 0
    46.     }
    47.  
    48. }
    49.  
    And where i call SpawnObject() from (player script for now)

    Code (CSharp):
    1.  private void TestSpawn()
    2. {
    3.  
    4.      if (Input.GetKeyDown(KeyCode.B))
    5.      {
    6.  
    7.          Ball.SpawnObject(ballSO);//working finds BallSO
    8.  
    9.      }
    10. }
    That references the ball script which has this:

    Code (CSharp):
    1.   public static void SpawnObject(ObjectSO objectSO)
    2.   {
    3.       NetworkSpawner.Instance.SpawnObject(objectSO);      
    4.   }

    All of the scripts are communicating with each other as expected because from the player script i am able to call SpawnObject() which should then call the SpawnObjectServerRpc() function. The print from SpawnObject() "Spawn Object Run" is outputted, however as you can see this is after where i call the ServerRpc function and i do not get the output "run!!!!!!!!" at all, let alone my ball spawning.

    What is going on? when i make it not a serverRpc function by renaming and removing the [ServerRpc] line or move the ball spawning code to the non serverRpc spawn object it works, however for obvious reasons in making a network game i need to use the serverRpc.

    What is wrong here? im finding it very hard to debug this issue as i cant see any reason the function wouldn't run at all and not give me any errors.

    Many Thanks,

    Dylan
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,992
    NetworkSpawner is on a GameObject that has a NetworkObject, and it is already in the networked scene? And you did load that scene with NetworkManager's network scene manager?

    If not, you would have to spawn the NetworkSpawner over the network by the server first.
     
  3. lazy_clap

    lazy_clap

    Joined:
    Jul 9, 2021
    Posts:
    7
    Network spawner script is on an object called network spawner with the network object component that is in the main game scene. I switch to this scene with either a start host or start client button from the main menu.

    here is my MenuUi script that switches to the scene with the network spawner object in it, where im trying to run the above code to spawn the ball

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class MenuUI : MonoBehaviour
    9. {
    10.    
    11.  
    12.  
    13.     public void StartHost()
    14.     {
    15.         HideMenu();
    16.         NetworkManager.Singleton.StartHost();
    17.         print("host pressed");
    18.     }
    19.  
    20.     public void StartClient()
    21.     {
    22.         HideMenu();
    23.         NetworkManager.Singleton.StartClient();
    24.         print("client pressed");
    25.     }
    26.  
    27.     private void HideMenu()
    28.     {  
    29.         SceneManager.LoadScene("GameWorld", LoadSceneMode.Single);
    30.        
    31.     }
    32.  
    33.    
    34. }
    35.  
    Is the issue you're talking about here?

    thanks :)
     
  4. lazy_clap

    lazy_clap

    Joined:
    Jul 9, 2021
    Posts:
    7
    sorry just realised i replied to the post not your comment :/
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,992
    You are loading a scene with the regular SceneManager and before you are actually starting the network connection. This scene will basically be offline, nothing in the scene gets synched.

    You need to first Start host or client, then use the NetworkManager's NetworkSceneManager to load the scene in online mode.
     
    lazy_clap likes this.
  6. lazy_clap

    lazy_clap

    Joined:
    Jul 9, 2021
    Posts:
    7
    Ok thanks, I will add the player movement syncing works though. I will try this tomorrow, what does loading the scene in online mode look like? Or what is the correct documentation for it? Sorry its my first time doing a multilayer project.
     
  7. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,992
  8. lazy_clap

    lazy_clap

    Joined:
    Jul 9, 2021
    Posts:
    7
    CodeSmile likes this.