2

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:

Prefabs

Community
  • 1
  • 1
Kiket95
  • 73
  • 10

1 Answers1

0

Your network object is your Player, so you need to route your network functions through your player.

Call CmdGetGun()* in your player class and call it from there.
*CmdGetGun() should not be a [Command]

Optionally, you can call CmdGetGun as a Command, but then you don't want networked instantiation, just local (the command will be called all clients, so all clients will locally create the gun).

EDIT
CmdGetGun will create the object on all clients, but it won't parent it like you are expecting. You should probably try the second approach I mentioned (call it as a command, but instantiate locally)

Mars
  • 2,505
  • 17
  • 26