- Home /
Spawning a child object with UNET
I've recently started adding co-op to my game using UNET, and I'm having real problems getting my head around it.
I'm currently just trying to set up a simple test scene.
I have a NetworkManager in the scene.
I have a prefab for the player, with a PlayerActor component based around NetworkBehaviour, as well as a NetworkIdentity with local authority enabled; this is registered in the NetworkManager as the player prefab.
I've also got a prefab with a WeaponActor and NetworkIdentity component on board. This is registered as spawnable.
All I want to do is provide the ability to add and remove instances of the WeaponActor prefab as a child of the PlayerActor - (I'll need local authority on the weapon, but I can come back to that..)
I've tried both of the approaches detailed here, but neither of them seem to be working properly for me. I just don't seem to be able to get this working at both client and host at the same time.
public class PlayerActor : NetworkBehaviour
{
[SerializeField]
private GameObject weaponPrefab;
public override void OnStartLocalPlayer()
{
this.Cmd_WeaponSpawn(this.weaponPrefab);
}
[Command]
private void Cmd_WeaponSpawn(GameObject weaponPrefab)
{
GameObject weapon = Object.Instantiate(weaponPrefab);
weapon.SetParent(this);
NetworkServer.Spawn(weapon);
this.Rpc_WeaponLink(weapon);
}
[ClientRpc]
private void Rpc_WeaponLink(GameObject weapon)
{
weapon.SetParent(this);
}
}
The specific problems vary, from Failed to spawn server object
to the prefab being null, but most often methods simply not executing.
How should I do this? Do I need to fix the above or switch to an entirely different approach?
Your answer

Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Simple Camera look script (MULTIPLAYER) 0 Answers
Rocket Jumping Woes 1 Answer
Detecting who left UNET game 3 Answers
is LLAPI bandwidth limited? 0 Answers