Search Unity

Projectile over network (Received NetworkTransform data for GameObject that doesn't exist)

Discussion in 'Scripting' started by Gogin, Aug 24, 2018.

  1. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    Hi

    I would like to ask you how to avoid error mentioned below. My situation is following, server generate "projectile" gameobject and this gameobject has own script which control movement over network.

    If target is not hit, it moves to target gameobject, also send RPC function to clients to update position. Once it hit target, all components are removed (works fine). When target is destroyed by other gameobject - projectile is destroyed (no more needed) - here is the issue (I destroy gameobject on server and some clients still received information about position for object which is no longer exist)

    Any idea?

    Code:

    Code (CSharp):
    1.     [ClientRpc]
    2.     public void RpcMoveTo(GameObject obj, Vector3 newPosition, Quaternion newQuaternion)
    3.     {
    4.         if (obj != null && obj.activeInHierarchy)
    5.         {
    6.             obj.transform.position = newPosition; //this will run in all clients
    7.             obj.transform.rotation = newQuaternion;
    8.         }
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.  
    14.         //Runs only fon server
    15.         if (!isServer)
    16.         {
    17.             return;
    18.         }
    19.  
    20.         //isnt target already dead and round is running
    21.         if (_target != null && _target.gameObject.activeInHierarchy && controler.isRoundRunning)
    22.         {
    23.  
    24.             //Target hit
    25.             if (isTargetHit)
    26.             {
    27.                 this.transform.parent = _target.transform;
    28.  
    29.                 Destroy(this.gameObject.GetComponent<Rigidbody>()); //destroy, no more needed
    30.                 Destroy(this.gameObject.GetComponent<BoxCollider>()); //destroy, no more needed
    31.                 Destroy(this.gameObject.GetComponent<Projectile>()); //destroy script, no more needed
    32.             }
    33.             else
    34.             {
    35.  
    36.                 transform.Translate(Vector3.forward * Time.deltaTime * projectileSpeed);
    37.                 transform.LookAt(targetRandomPosition());
    38.  
    39.                 //TODO: Debug!
    40.                 //if(NetworkServer.FindLocalObject(this.GetComponent<NetworkIdentity>().netId))
    41.                 //{
    42.                     RpcMoveTo(this.gameObject, this.transform.position, this.transform.rotation); //Send information to clients
    43.                 //}
    44.             }
    45.         }
    46.         else
    47.         {
    48.             NetworkServer.Destroy(this.gameObject);
    49.         }
    50.     }
    Error:

    Received NetworkTransform data for GameObject that doesn't exist UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()