I have got my Hand Prefab attached to Player Prefab as a child. I want to spawn Gun Prefab in my Hand Prefab(Players child). Is this way i can do this from Hand level?
At the moment I am spawning Gun from Hand, but it works only on single Player. While i play with 2 players, in the moment of spawning a Gun. Game crashes for client. This is the Error im getting:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkServer.SpawnWithClientAuthority (UnityEngine.GameObject obj, UnityEngine.Networking.NetworkConnection conn) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1565)
HandHolder.CmdGetGun () (at Assets/Scripts/HandHolder.cs:27)
HandHolder.Update () (at Assets/Scripts/HandHolder.cs:19)
Thats my Code for Hand:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class HandHolder : NetworkBehaviour {
[SerializeField] GameObject gun;
private GameObject playerGun;
// Update is called once per frame
void Update () {
if(GetComponentInParent<NetworkIdentity>().isLocalPlayer)
{
if (playerGun)
{
playerGun.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
playerGun.transform.rotation = transform.rotation;
}
else if (Input.GetKeyDown(KeyCode.I))
{enter code here
CmdGetGun();
}
}
}
// [Command]
public void CmdGetGun()
{
playerGun = Instantiate(gun, transform.position, transform.rotation) as GameObject;
NetworkServer.SpawnWithClientAuthority(playerGun, GetComponentInParent<NetworkIdentity>().connectionToClient);
}
}
When im adding [Command] before CmdGetGun() method, in the moment of spawning a gun i got this error:
There is no NetworkIdentity on this object. Please add one.
UnityEngine.Networking.NetworkBehaviour:get_isServer()
HandHolder:CallCmdGetGun()
HandHolder:Update() (at Assets/Scripts/HandHolder.cs:19)
But when i add NetworkIdentity to my child Hand Prefab it shows me that hasAuthority, isLocalPlayer is False for both Hand and Player.
I have no idea how can i spawn Gun in Hand Prefab.
Here are Components and tree: