I am aware that in order to spawn objects over the network, one must call NetworkServer.Spawn() on the Server. However, say for example the client fires a projectile on mouse click. On the client side, we don’t want to wait for the server to receive the command to spawn the projectile but rather spawn the projectile instantly on the client and then notify the server.
How can I go about doing this? (NetworkServer.Spawn() does not work on the client side)
I have tried your code before and it doesn’t work. When the client attempts to spawn a projectile, the server throws an error (not set to an instance of an object). It seems that Unet doesn’t like having Gameobjects as arguments to RPC’s? I am using Unity 5.1.3
Not sure if either of you found a solution to this problem. I am trying to do the same thing and also get the “Object reference not set to an instance of an object” error. Everything works when creating the object on the host client, but not on any other. Here is my code:
void Update() {
if (isLocalPlayer) {
if (Input.GetKeyDown (KeyCode.E)) {
GameObject tempProjectile = Instantiate(fireballPrefab, fireballStartObject.transform.position, Quaternion.identity) as GameObject;
CmdShoot (tempProjectile);
}
}
}
[Command]
void CmdShoot(GameObject projectile){
NetworkServer.Spawn(projectile);
}
Any idea what could be going wrong here? The fireballPrefab exists in the Registered Spawnable Prefabs of the network manager.
You cannot send reference types to the server in commands. Anything you send in a command needs to be serialized and then sent to the server. Doing that with something like a gameObject is apparently not supported in the HLAPI. The only way I have been able to track object between clients is using networkIDs (which are functionally strings).
UNET Command parameters appear to be limted to int, float, enum, string, Vect2, Vect3, Vect4 and user defined structs.
Are you getting the object null error on Line 6 or line 13 in the above reference? There’s a cast happening on line 6 that could possibly come out null.
All variables passed around in C# are byvalue, by default. Reference objects are only passed as reference types if you explicitly state ref or out.
But…it still should be serializable if you pas it by value. Try something like this
public void Update()
{
if (isLocalPlayer){
if (Input.GetKeyDown(KeyCode.E))
{
GameObject tempProjectile = (GameObject)Instantiate(fireballPrefab, fireballStartObject.transform.position, Quaternion.identity);
CmdShoot (tempProjectile);
}
}
}
public void CmdShoot(GameObject projectile){
if (projectile == null)
{
Debug.Log("No game object was received from the client to the server method");
return;
}
NetworkServer.Spawn(projectile);
}
Two things happen here. 1, we use a non-nullable cast into tempProjectile, if it fails there, it should throw an exception. Secondly, we make sure we’re getting something in the server command.
@Freakyuno I have tried everything. If I am calling command function from both server and client but on client it is not working.
In my game GameObject is colliding with some element let say food. When it collide with food, it instantiate something. code is working fine on server side, but not on Client. Following is my code :-