Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Handling Explosions of PlayerSpawned Objects? "Trying to send command for object without authority."

Discussion in 'Multiplayer' started by CatDoge, Sep 22, 2018.

  1. CatDoge

    CatDoge

    Joined:
    Aug 8, 2014
    Posts:
    13
    So I want to spawn an explosion from a missile that the player shoots.

    If I try to add the "explosion" like this:

    (it is actually for a mine but it is actually the same)

    Code (CSharp):
    1.     void OnCollisionEnter(Collision coll)
    2.     {
    3.         Debug.Log("playercollision");
    4.  
    5.         if (coll.gameObject.tag == "Player")
    6.         {
    7.             CmdOnCollision();
    8.             NetworkServer.Destroy(gameObject);
    9.             col = true;
    10.         }
    11.  
    12.      
    13.     }
    14.  
    15.     [Command]
    16.     void CmdOnCollision()
    17.     {
    18.  
    19.         expo = (GameObject)Instantiate(
    20.              exp,
    21.             minecoll.transform.position,
    22.             minecoll.transform.rotation);
    23.  
    24.  
    25.         NetworkServer.Spawn(expo);
    26.  
    27.  
    28.  
    29.     }
    it give me the following error

    "Trying to send command for object without authority."

    I actually have a workaround, where I check the collision of the missile with a static variable and then add the explosion prefab over the shooting script.
    But there are 2 problems,

    1st I can only instantiate one missile at once. If I instantiate more then one missile I only can destroy the last missile because the script loses the properties of the others.

    2nd I want to have the bullets/missiles/mine etc. instantiate their explosions themselves and destroy themselves too.

    How can I achieve giving my objects player authority?
     
  2. CatDoge

    CatDoge

    Joined:
    Aug 8, 2014
    Posts:
    13
    got it fixed by myself

    spawning with
    NetworkServer.SpawnWithClientAuthority(expo, m_Identity);


    getting the identity with

    m_Identity = obj.GetComponent<NetworkIdentity>().connectionToClient;