Search Unity

Third Party Mirror Spawning prefab from Client with client authority enabled crash due to command parameters bec

Discussion in 'Multiplayer' started by Deamonpog, Feb 21, 2022.

  1. Deamonpog

    Deamonpog

    Joined:
    Oct 8, 2013
    Posts:
    10
    Mirror Spawning prefab from Client with client authority enabled crash due to command parameters become null

    My code in player prefab has a component script that contains the following for spawning a bullet.
    It's using 'Mirror'. The 'NetworkTransform' has `ClientAuthority` enabled.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (!isLocalPlayer)
    4.         {
    5.             return; // dont run if not the local player
    6.         }
    7.  
    8.         // .... some code....
    9.  
    10.         UpdateAction();
    11.  
    12.         // ..... more code ....
    13.     }
    14.  
    15.     void UpdateAction()
    16.     {
    17.         if (targetOther != null)
    18.         {
    19.             Shoot();
    20.             targetOther = null;
    21.         }
    22.     }
    23.  
    24.     void Shoot()
    25.     {
    26.         print("Bullet in Shoot : " + bullet + "\n is null ? " + (bullet == null));
    27.         CmdShoot(bullet, transform.position + new Vector3(0, 2, 0), targetOther.gameObject);
    28.     }
    29.  
    30.     [Command]
    31.     public void CmdShoot(GameObject bulletPrefab, Vector3 spawnPos, GameObject targetOther)
    32.     {
    33.         print("Bullet Prefab : " + bulletPrefab +"\n is null ? " + (bulletPrefab == null));
    34.         GameObject go = Instantiate(bulletPrefab);
    35.         print(go);
    36.         go.transform.position = spawnPos;
    37.         HomingAttack ha;
    38.         if (go.TryGetComponent<HomingAttack>(out ha))
    39.         {
    40.             ha._target = targetOther;
    41.         }
    42.         NetworkServer.Spawn(go);
    43.     }
    The
    GameObject bullet
    is a variable defined as
    public GameObject bullet;
    in the script. I have assigned this variable a proper prefab which has a NetworkIdentity and a NetworkTransform. Also, the prefab is added to registered spawnable prefabs.

    The problem is the code crashes when this part of code is run ( it runs when I right click on another player ) since it reports that the `bullet` variable is null.
    I have checked this with the print statements on line 26 and 33.
    When run by the host-server player: line 26 prints that it has a valid value but line 33 prints that it is null.
    How should I fix this?
    Thank you.
     
    Last edited: Feb 22, 2022
  2. Deamonpog

    Deamonpog

    Joined:
    Oct 8, 2013
    Posts:
    10
    Solved it.
    Why the error occurred was that the prefab I am passing is not initialized in the server (instead I had it initialized in the client !!! which is wrong for my purpose).

    Solution:

    Instead of passing the `GameObject` prefab instance from the client to server, I must pass the index of the prefab from the registered spawnable prefabs.
    Then in the server (command function) I locate the prefab instance by using `NetworkManager.singleton.spawnPrefabs[index_of_bullet_prefab]`


    So for example the code is now like this:

    Code (CSharp):
    1.  
    2. void Shoot()
    3.     {
    4.         CmdShoot(0, transform.position + new Vector3(0, 2, 0), targetOther.gameObject);
    5.     }
    6.     [Command]
    7.     public void CmdShoot(int prefabIdx, Vector3 spawnPos, GameObject targetOther)
    8.     {
    9.  
    10.         GameObject go = Instantiate(NetworkManager.singleton.spawnPrefabs[prefabIdx]);
    11.         print(go);
    12.         go.transform.position = spawnPos;
    13.         HomingAttack ha;
    14.         if (go.TryGetComponent<HomingAttack>(out ha))
    15.         {
    16.             ha._target = targetOther;
    17.         }
    18.         NetworkServer.Spawn(go);
    19.     }
    This issue was not there for `targetOther` variable, I think this is because that gameObject exists in the server. Currently all my `targetOther` values are players (clients). Maybe that is why I can use that without a problem on contrast.
     
    Uglysluggers and GamesbyJP like this.