- Home /
How can I spawn a GameObject on all players in my multiplayer game?
Hi everyone,
I'm currently working on a game, but I'm running in to a problem.
On the beginning of the scene (being redirected from the lobby), I am trying to spawn a custom weapon prefab on every connected player. This works on the host (who is playing as well), but not on the built client. The built client can see the weapon from the host, but the host can't see the built client's weapon since it isn't there.
Here is the piece of code that I use to instanciate the weapon.
[Command]
void CmdInstantiateWeapon()
{
Debug.Log(currentWeapon);
Debug.Log(weaponProperties);
Debug.Log(FPSCam);
GameObject personalWeaponInstance = Instantiate(currentWeapon, weaponProperties.hipPos, Quaternion.identity, FPSCam);
personalWeaponInstance.transform.localPosition = weaponProperties.hipPos;
NetworkServer.Spawn(personalWeaponInstance);
}
Some thing I should note though, is that the debug is telling if the object isn't null. The host is getting the names from the debug, but when the client spawns in, the debug will return null. CurrentWeapon is the prefab of the weapon I'm trying to spawn, weaponProperties is the script attached to the weapon keeping the properties of the weapon and FPSCam is the transform on which the weapon should be attached.
Ohh, and I am using UNet which works for what I want, except for this.
Thanks ;)
Did you register your weapon as spawnable object in your network manager?
Also you should probably assign local authority to those weapons.
Yes, I have added it to the spawnable objects in the Lobby $$anonymous$$anager and the weapon prefab has a Network Identity (and transform) with the Local Player Authority being turned on on the Identity...
I'm not sure, but for Local Player Authority you have to also assign a player that has the authority, checkbox is not enough
https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity.AssignClientAuthority.html