Search Unity

Trying to send command for object without authority.(Warning)

Discussion in 'Multiplayer' started by RoryScanlan, Feb 6, 2017.

  1. RoryScanlan

    RoryScanlan

    Joined:
    Jan 25, 2014
    Posts:
    139
    Hey, i am new to Unet so please be patient :p

    I am trying to create a system where weapon spawns on the floor, which can be picked up and will respawn after some time(Like quake 3)

    The scripts i have created seem to work perfectly.

    However when an external client(not hosts) picks up a weapon they get this warning
    which is pointing towards the 'WeaponPickup' script(2nd on this page)


    Here are the scripts

    Weapon Spanwer...
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class Weapon_Spawner : NetworkBehaviour {
    7.  
    8.     //THIS IS A SERVER ONLY OBJECT
    9.     //THIS OBJECT ONYL APPEARS ON THE SERVERS CLIENT
    10.     //NETWORKSERVER CALLS CAN THEREFORE BE MADE WITHOUT A [Command]
    11.     //OBJECTS SPAWNED THIS WAY WILL BE HANDLE BY 'UNET' SPAWNED ITEMS LIST AND THEREFORE SYNCED ACROSS CLIENTS
    12.  
    13.     [SerializeField]
    14.     private GameObject GunToSpawn;
    15.     [SerializeField]
    16.     private int SpawnDelay;
    17.     private GameObject instantiatedGun;
    18.     private bool collected = false;
    19.  
    20.     public override void OnStartServer()
    21.     {
    22.         SpawnWeapon();
    23.     }
    24.  
    25.     public void SpawnWeapon()
    26.     {
    27.         instantiatedGun = (GameObject)Instantiate(GunToSpawn, transform.position, transform.rotation);  
    28.         NetworkServer.Spawn(instantiatedGun);
    29.     }
    30.  
    31.     private void ResetSpawer()
    32.     {
    33.         collected = false;
    34.         SpawnWeapon();
    35.     }
    36.  
    37.     private void OnTriggerEnter(Collider other)
    38.     {
    39.         if(other.tag == "Player" && collected == false)
    40.         {
    41.             collected = true;
    42.             Debug.Log("Collected");
    43.             Invoke("ResetSpawer", SpawnDelay);
    44.         }
    45.     }
    46. }
    47.  
    Spawned Weapon
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class WeaponPickup : NetworkBehaviour {
    7.  
    8.     public Weapon pickUpGun;
    9.     private bool collected = false;
    10.     public Weapon_Spawner mySpawner;
    11.  
    12.     void OnTriggerEnter(Collider col)
    13.     {
    14.         if(col.transform.tag == "Player" && collected == false)
    15.         {
    16.             collected = true;
    17.             CmdRequestGiveWeapon(col.gameObject);
    18.         }
    19.     }
    20.  
    21.     [Command]
    22.     void CmdRequestGiveWeapon(GameObject _player)
    23.     {
    24.         gameObject.GetComponent<NetworkIdentity>().AssignClientAuthority(_player.GetComponent<NetworkIdentity>().connectionToClient);
    25.         RpcGiveWeapon(_player);
    26.     }
    27.  
    28.     [ClientRpc]
    29.     void RpcGiveWeapon(GameObject _player)
    30.     {
    31.         _player.GetComponent<WeaponManager>().AddWeaponToPlayersInventory(pickUpGun);
    32.         Destroy(gameObject);
    33.     }
    34. }
    35.  
    If any one can give advice/help/tips its already appreciated :)
     
    Last edited: Feb 6, 2017
  2. wadybaby7861

    wadybaby7861

    Joined:
    Apr 17, 2020
    Posts:
    4
    That is why i dont use Unity anymore, no help............... for years
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It has been answered in lots of other threads (I've personally answered it in at least a dozen), and the networking API used here was deprecated years ago. The answer is also in the documentation.

    But hear you go.... You can only send Commands for networked objects your client has been assigned authority, or that client's special Player GameObject. If you're getting this warning you're trying to send a Command on an object the client does not have authority over.

    So there are several ways to handle this. You can have the host assign authority over this specific networked object to this specific client prior to issuing the Command. You can use the Player GameObject to instead send the Command. Or you can instead not send a Command at all, but a Unet Message. A Unet Message isn't associated with any networked objects at all, so the object authority system doesn't apply.

    I normally would use the Player GameObject to send the Command, but sending a Message makes sense in a lot of cases (it can just be a bit more hassle to set up). Assigning authority just to issue a Command leads to madness.
     
    Azurne likes this.
  4. wadybaby7861

    wadybaby7861

    Joined:
    Apr 17, 2020
    Posts:
    4
    Did not mean to be nasty or anything, but I was getting very upset why I had so much issues. I switched from Mirror back to the OLD DEPRECATED Unet o_O and I have had better luck.

    Did exactly as you said.

    Thanks;)
     
    Joe-Censored likes this.