I am currently creating a two-person multiplayer game and have stumbled upon a problem when trying to spawn a game object from the client.
In my Defender class I have a function which gets called upon each time a player recruits a unit.
void RecruitDefender(DefenderBlueprint blueprint)
{
defender = blueprint.gameObject;
Player.instance.CmdSpawnDefender(defender, transform.position, transform.rotation);
}
If I spawn the units in this RecruitDefender-function they spawn just fine, but they will only spawn locally. In order to make them appear for both server and client I tried to instantiate the unit from the player class through a [Command]:
public class Player : NetworkBehaviour
{
[Command]
public void CmdSpawnDefender(GameObject defender, Vector3 position, Quaternion rotation)
{
GameObject _defender = (GameObject)Instantiate(defender, position, rotation);
NetworkServer.Spawn(_defender);
}
However, this gives me the error "The Object you want to instantiate is null" when trying to spawn from the client; the player located on the server can spawn without any problems. The problem seems to stem from the fact that the game object "defender" is null for the client in the Player class, which seems strange as it has a value in the RecruitDefender class. As far is I have understood Unet (which I admit is limited) you should be able to pass game objects to [Command] functions. Does anyone have an idea of what the problem could be here?