- Home /
Calling Command Function from Client results in "NetworkServer not active..."
Hey!
I have an issue when executing a command from a client. I want to spawn an object using the following code:
[Command]
public void CmdHandleTap()
{
GameObject instance = Instantiate(projectilePrefab, transform.Find("ProjectileSpawn"));
instance.GetComponent<Rigidbody>().AddForce(transform.forward * 1000 * instance.GetComponent<Projectile>().speed);
NetworkServer.Spawn(instance);
}
However I get the following error when NetworkServer.Spawn(...) is called:
SpawnObject for Projectiles(Clone) (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server. UnityEngine.Networking.NetworkServer:Spawn(GameObject) PlaneController:CmdHandleTap() (at Assets/Scripts/GameObjects/PlaneController.cs:82)
The following things I consider noteworthy:
I can spawn a projectile if using a host but only on the host client.
Projectile has a NetworkIdentity and a NetworkTransform Component attached.
The problem persists for different clients on the same machine as well as different clients on different machines
I have been following the UNET tutorial on the official website and spawning objects there was working... I don't find a difference to what I am doing though.
I'm using an Interface to access the command functions.
Bottom Line: For some reason I cannot spawn prefabs in a Command Function when called from a client. Can anyone see what I am missing here?
Note: I have noticed the same question being asked a few times already, eg. here. However I have not found an answer yet. Maybe someone can help by now?
Answer by schoschi · Nov 28, 2017 at 10:07 PM
I managed to solve the issue. My problem was that I tried to access command methods through an interface.
2 possible solutions:
Remove the interface
do a two step implementation of the interface methods like this:
Code:
public interface IController
{
void HandleTap();
}
public class MyController : NetworkBehaviour, IController
{
//This will still be executed by the client
public void HandleTap()
{
CmdHandleTap();
}
[Command]
private void CmdHandleTap()
{
//Code from above
}
}
I hope this might help people looking for a similar answer in the future!
Your answer
Follow this Question
Related Questions
Cmd is called on Server ERROR!?!! 0 Answers
UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer
UnityScript not recognizing NetworkServer "Unknown Identifier" 0 Answers
Why will the raycast not work on the client? 2 Answers
zombie network spawn! help" 0 Answers