Search Unity

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

Question Why can't I use Server RPCs in seperate scripts?

Discussion in 'Netcode for GameObjects' started by NexiNex, Jul 22, 2023.

  1. NexiNex

    NexiNex

    Joined:
    Jun 4, 2020
    Posts:
    10
    Not sure if this is supposed to be "bug" or "question" but I'm guessing bug is for actual issues with the unity product while this is more likely to be my own error. I've attached the two scripts that should work together, and the error given is (lines 16 and 17 of client handler): The name 'testServerRpc' does not exist in the current context. These exist cause I was trying to test to find the solution for a different issue, but they also just make a basic ping for right now, and their actual contents shouldn't be too relevant, but I'm including just incase. I have multiplayer tools and netcode for game objects installed. Each of the game objects each script is attached to has the network object script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class clientHandler : NetworkBehaviour
    7. {
    8.     public   NetworkVariable<bool> hasPinged = new NetworkVariable<bool>();
    9.     public int pingCount;
    10.     void Start()
    11.     {
    12.         if(!IsServer)
    13.         {
    14.             NetworkVariable<bool> hasPinged = new NetworkVariable<bool>();
    15.         }
    16.         testServerRpc();
    17.         testServerRpc();
    18.         Debug.Log(hasPinged.Value);
    19.     }
    20. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class serverScript : NetworkBehaviour
    7. {
    8.     public   NetworkVariable<bool> hasPinged = new NetworkVariable<bool>();
    9.     public int pingCount;
    10.     [ServerRpc(RequireOwnership=false)]
    11.     public void testServerRpc()
    12.     {
    13.         //ServerRpcParams.Receive.SenderClientId
    14.         if (NetworkManager.ConnectedClients.ContainsKey(OwnerClientId))
    15.         {
    16.             var client = NetworkManager.ConnectedClients[OwnerClientId];
    17.             if(hasPinged.Value==false)
    18.             {
    19.                 pingCount+=1;
    20.                 Debug.Log("I have been pinged " +pingCount);
    21.              
    22.             }
    23.             hasPinged.Value=true;
    24.          
    25.         }
    26.     }
    27. }
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Try:
    serverScript.testServerRpc();