/// <summary>
    /// Destroys a network object. To be used in conjunction with NetworkInstantiate
    /// </summary>
    /// <param name="obj"></param>
    public void NetworkDestroy(NetObjectID obj)
    {
        NetworkID id = obj.netID;

        if (id.type != NetworkIDType.game)
        {
            Debug.LogError("Tried to destroy an ID that was not a game ID");
            return;
        }

        // remove all buffered messages assigned to this object
        foreach (BufferedMessage msg in obj.bufferedMessages)
        {
            bufferedMessages.Remove(msg);
        }
        // create a destroy event for all clients
        NetWriter writer = StartNetworkMessage("cl_NetworkDestroy", thisNetworkID);

        id.WriteBytes(writer);
        // notify all clients to destroy this object
        SendMessageToOtherClients(writer, NetworkCore.AllCostMsg, false);
        // finally destroy this object
        Destroy(obj.gameObject);
        // unregister this ID
        gameIDs[id.idNumber].left  = null;
        gameIDs[id.idNumber].right = false;
    }
    public static NetWriter StartNetworkMessage(string functionName, NetworkID senderID)
    {
        if (writer.isWriting)
        {
            Debug.LogWarning("Tried to start writing another message when one is already being written. Please finish and send the first message before starting another one.");
            return(null);
        }
        writer.Reset(writeData, NetworkCore.MAX_DATA_SIZE);
        writer.StartWriting();
        // write the sender id
        senderID.WriteBytes(writer);
        // write the destination function id
        ushort functionID = (ushort)FindIdForName(functionName);

        writer.WriteUShort(functionID);
        return(writer);
    }
    /// <summary>
    /// Instantiates a prefab and assigns a network ID to it.
    /// This is replicated across all peers, and is buffered for any new players.
    /// To destroy these objects, you should use the "NetworkDestroy" function.
    /// </summary>
    /// <param name="pos">Position.</param>
    /// <param name="rot">Rot.</param>
    public GameObject NetworkInstantiate(NetworkPrefab prefab, Vector3 pos, Quaternion rot, bool sendToLocalClient)
    {
        if (NetworkCore.isServer)
        {
            if (prefab == null)
            {
                Debug.LogError("NetworkInstantiate called with no prefab. Object not created");
                return(null);
            }
            GameObject  newObj = Instantiate(prefab.serverPrefab, pos, rot);
            NetObjectID idComp = newObj.GetComponent <NetObjectID>();
            NetworkID   newID  = GetNextID(idComp);
            idComp.SetNetID(newID);
            NetWriter writer = StartNetworkMessage("cl_NetworkInstantiate", thisNetworkID);
            writer.WriteInt(prefab.id);
            writer.WriteVector3(pos);
            writer.WriteVector3(rot.eulerAngles);
            newID.WriteBytes(writer);
            BufferedMessage msg = null;
            if (sendToLocalClient)
            {
                msg = SendMessageToAllClients(writer, NetworkCore.AllCostMsg, true);
            }
            else
            {
                msg = SendMessageToOtherClients(writer, NetworkCore.AllCostMsg, true);
            }

            if (msg == null)
            {
                Debug.Log("Failed to successfully send Instantiation message. Unregistering Network ID: " + newID.ToString());
                FreeNetworkID(idComp);
                return(newObj);
            }
            idComp.AddBufferedMessage(msg);
            newObj.BroadcastMessage("OnNewNetworkSceneLoaded");
            return(newObj);
        }
        return(null);
    }