Hi, I have a problem that when I call a function with ServerRpc, it’s not doing anything. No error messages are visible in the console. Here is the code:
void OnMouseDown()
{
Debug.Log("Clicked");
SpawnPieceServerRpc();
}
[ServerRpc(RequireOwnership = false)]
private void SpawnPieceServerRpc()
{
Debug.Log("Spawned");
GameObject pieceGO = (GameObject)Instantiate(pieceToSpawn, transform.position, Quaternion.identity);
pieceGO.GetComponent<NetworkObject>().Spawn();
}
This script has NetworkBehaviour, a Network object is also attached to the object. The object that I’m trying to spawn also has the network object and is in the network prefabs in network manager.
So every time I click on this object, another should spawn in it’s place, but nothing happens. Console only writes “Clicked” but not “Spawned”. Any ideas?
Hmmm I can only think of the client not being connected respectively the host hasn’t run the StartHost() method yet. Or the script that you’re running isn’t actually on a NetworkObject - it may be on one but perhaps the script that’s actually running is also somewhere else, perhaps on the GUI?
If it’s set up as you describe it should work but maybe double-check and verify that the Console doesn’t have the display of warning/error messages disabled.
Hi, thank you for your reply.
I checked everything you said, but everything should be ok. Although it might be worth mentioning that the game object with this script is getting instantiated by another object. I’m am not spawning the object because it should only be visible to the owner.
After further testing I figured out that when I spawn this object in before I create the host, everything is working (not when I create it, then connect through client, this way it still doesnt work). I need this to be also be working when the player is already connected (to instantiate this object which is visible only to the player that spawned it and when he clicks on it, an object spawns that is visible to all the other players).
Hi,
turns out spawning this object right before running the ServerRpc works like I wanted it to. I guess only spawned network objects can access ServerRpc.