Search Unity

host id {0} has been already deleted

Discussion in 'Multiplayer' started by any_user, Jul 16, 2015.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    When changing scenes, I get this error:
    host id {0} has been already deleted

    But it doesn't show any stack trace that could lead me to the issue, and everything seems to work correctly. I think it happens since I converted an old NetworkRigidbody script to the new networking, but I'm not sure if that's the problem.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4. public class NetworkRigidbody : NetworkBehaviour
    5. {
    6.  
    7.     Rigidbody2D _rigidbody2D;
    8.     public new Rigidbody2D rigidbody2D {
    9.         get {
    10.             if (!_rigidbody2D)
    11.                 _rigidbody2D = GetComponent<Rigidbody2D> ();
    12.             return _rigidbody2D;
    13.         }
    14.     }
    15.  
    16.     float interpolationBackTime = .15f;
    17.     int m_ExtrapolationLimitMS = 200;
    18.  
    19.     [System.Serializable]
    20.     internal struct  State
    21.     {
    22.         internal int timestamp;
    23.         internal Vector2 position;
    24.         internal float rotation;
    25.         internal Vector2 velocity;
    26.         internal float angularVelocity;
    27.  
    28.         public static void Serialize(State state, NetworkWriter writer)
    29.         {
    30.             writer.Write (state.timestamp);
    31.             writer.Write (state.position);
    32.             writer.Write (state.rotation);
    33.             writer.Write (state.velocity);
    34.             writer.Write (state.angularVelocity);
    35.         }
    36.  
    37.         public static State Deserialize(NetworkReader reader)
    38.         {
    39.             State state = new State ();
    40.             state.timestamp = reader.ReadInt32();
    41.             state.position = reader.ReadVector2();
    42.             state.rotation = reader.ReadSingle();
    43.             state.velocity = reader.ReadVector2 ();
    44.             state.angularVelocity = reader.ReadSingle ();
    45.             return state;
    46.         }
    47.     }
    48.  
    49.     // We store twenty states with "playback" information
    50.     State lastSentState;
    51.     State[] m_BufferedState = new State[20];
    52.     // Keep track of what slots are used
    53.     int m_TimestampCount;
    54.  
    55.     public override float GetNetworkSendInterval ()
    56.     {
    57.         return 1 / 20f;
    58.     }
    59.  
    60.     public override int GetNetworkChannel ()
    61.     {
    62.         return 2;
    63.     }
    64.  
    65.     public override bool OnSerialize (NetworkWriter writer, bool initialState)
    66.     {
    67.         lastSentState = GetCurrentState ();
    68.         State.Serialize (lastSentState,writer);
    69.  
    70.         base.OnSerialize (writer, initialState);
    71.  
    72.         return true;
    73.     }
    74.  
    75.     public override void OnDeserialize (NetworkReader reader, bool initialState)
    76.     {
    77.         State state = State.Deserialize(reader);
    78.  
    79.         ReceiveNewState (state);
    80.  
    81.         base.OnDeserialize (reader, initialState);
    82.     }
    83.  
    84.     void FixedUpdate()
    85.     {
    86.         if (isServer)
    87.             FixedUpdateServer ();
    88.     }
    89.  
    90.     void FixedUpdateServer()
    91.     {
    92.         SetDirtyBit (1);
    93.     }
    94.  
    95.     // We have a window of interpolationBackTime where we basically play
    96.     // By having interpolationBackTime the average ping, you will usually use interpolation.
    97.     // And only if no more data arrives we will use extra polation
    98.     void Update ()
    99.     {
    100.         if (isClient && !isServer)
    101.             UpdateClient ();
    102.     }
    103.  
    104.     void UpdateClient()
    105.     {
    106.         byte error;
    107.         int delayMS = NetworkTransport.GetRemoteDelayTimeMS (
    108.             Game.networkManager.client.connection.hostId,
    109.             Game.networkManager.client.connection.connectionId,
    110.             NetworkTransport.GetNetworkTimestamp (),
    111.             out error);
    112.         // !!!! this is wrong at the beginning, ok after 15-20 s (should be fixed by unity)
    113.         // http://issuetracker.unity3d.com/issues/networktransport-dot-getremotedelaytimems-does-not-work-correctly-on-clients
    114.  
    115.         int networkTime = (NetworkTransport.GetNetworkTimestamp () + delayMS);
    116.  
    117.         // This is the rigidbody playback time of the rigid body
    118.     //    Debug.Log("" +  " - " + Network.time + " - " + interpolationTime);
    119.  
    120.         int rtt = NetworkTransport.GetCurrentRtt (
    121.             Game.networkManager.client.connection.hostId,
    122.             Game.networkManager.client.connection.connectionId,
    123.             out error);
    124.  
    125.         float interpolationBackTimeTarget = Mathf.Max( (rtt+15)/1000f, GetNetworkSendInterval()*2); // minimum 1 / sendrate * 2 to fix jitter (?)
    126.  
    127.         interpolationBackTime = Mathf.Lerp( (float)interpolationBackTime, (float)interpolationBackTimeTarget, Time.deltaTime * 0.3f);
    128.  
    129.         int interpolationTime = networkTime - Mathf.RoundToInt(interpolationBackTime*1000);
    130.  
    131.         if (m_TimestampCount == 0)
    132.             return;
    133.  
    134.         // Use interpolation if the rigidbody playback time is present in the buffer
    135.         if (m_BufferedState[0].timestamp > interpolationTime)
    136.         {
    137.             // Go through buffer and find correct state to play back
    138.             for (int i=0;i<m_TimestampCount;i++)
    139.             {
    140.                 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount-1)
    141.                 {
    142.                     // The state one slot newer (<100ms) than the best playback state
    143.                     State rhs = m_BufferedState[Mathf.Max(i-1, 0)];
    144.                     // The best playback state (closest to 100 ms old (default time))
    145.                     State lhs = m_BufferedState[i];
    146.                  
    147.                     // Use the time between the two slots to determine if interpolation is necessary
    148.                     int length = rhs.timestamp - lhs.timestamp;
    149.                     float t = 0.0F;
    150.                     // As the time difference gets closer to 100 ms t gets closer to 1 in
    151.                     // which case rhs is only used
    152.                     // Example:
    153.                     // Time is 10.000, so sampleTime is 9.900
    154.                     // lhs.time is 9.910 rhs.time is 9.980 length is 0.070
    155.                     // t is 9.900 - 9.910 / 0.070 = 0.14. So it uses 14% of rhs, 86% of lhs
    156.                     if (length > 0.0001)
    157.                         t = (float)((interpolationTime - lhs.timestamp) / (float)length);
    158.                  
    159.                     // if t=0 => lhs is used directly
    160.                     rigidbody2D.position = Vector2.Lerp(lhs.position, rhs.position, t);
    161.                     rigidbody2D.rotation = Mathf.LerpAngle(lhs.rotation, rhs.rotation, t);
    162.                     rigidbody2D.velocity = Vector2.Lerp(lhs.velocity,rhs.velocity, t);
    163.                     rigidbody2D.angularVelocity = Mathf.Lerp(lhs.angularVelocity, rhs.angularVelocity, t);
    164.                     //Debug.Log("Found packet for: " + gameObject.name);
    165.                     //DebugExtension.DebugPoint (rigidbody2D.position,Color.green,1,2);
    166.                     return;
    167.                 }
    168.             }
    169.          
    170.          
    171.             Debug.Log("Did not find packet " + m_TimestampCount + " for: " + gameObject.name);
    172.  
    173.          
    174.         }
    175.         // Use extrapolation
    176.         else
    177.         {
    178. //            print ("EXTRAPOLATE");
    179.             State latest = m_BufferedState[0];
    180.  
    181.             float extrapolationLengthMS = (float)(interpolationTime - latest.timestamp);
    182.  
    183.             // Don't use extrapolation for more than xx ms, you would need to do that carefully
    184.             //if (extrapolationLengthMS < m_ExtrapolationLimitMS)
    185.             //{
    186.             //    rigidbody2D.position = latest.position + latest.velocity * extrapolationLengthMS / 1000;
    187.             //    rigidbody2D.rotation = latest.rotation + latest.angularVelocity * extrapolationLengthMS / 1000;
    188.             //}
    189.             //else
    190.             //{
    191.                 rigidbody2D.position = latest.position;
    192.             rigidbody2D.rotation = latest.rotation;
    193.             rigidbody2D.velocity = latest.velocity;
    194.             rigidbody2D.angularVelocity = latest.angularVelocity;
    195.             //}
    196.  
    197.             //DebugExtension.DebugPoint (rigidbody2D.position,Color.blue,1,2);
    198.         }
    199.     }
    200.  
    201.     State GetCurrentState()
    202.     {
    203.         State s = new State ();
    204.         s.timestamp = NetworkTransport.GetNetworkTimestamp ();
    205.         s.position = rigidbody2D.position;
    206.         s.rotation = rigidbody2D.rotation;
    207.         s.velocity = rigidbody2D.velocity;
    208.         s.angularVelocity = rigidbody2D.angularVelocity;
    209.         return s;
    210.     }
    211.  
    212.     //This Function inserts the new State in the proper slot.
    213.     //This is the Insertion-Sort
    214.     private void ReceiveNewState( State newState )
    215.     {
    216.         //If no States are present, put in first slot.
    217.         if( m_TimestampCount == 0 )
    218.         {
    219.             m_BufferedState[0] = newState;
    220.         }
    221.         else
    222.         {
    223.  
    224.             //First find proper place in buffer. If no place is found, state can be dropped (newState is too old)
    225.             for( int i = 0; i < m_TimestampCount; i ++ )
    226.             {
    227.                 //If the state in slot i is older than our new state, we found our slot.
    228.                 if( m_BufferedState[i].timestamp < newState.timestamp )
    229.                 {
    230.                     // Shift the buffer sideways, to make room in slot i. possibly deleting state 20
    231.                     for (int k=m_BufferedState.Length-1;k>i;k--)
    232.                     {
    233.                         m_BufferedState[k] = m_BufferedState[k-1];
    234.                     }
    235.  
    236.                     //insert state
    237.                     m_BufferedState[i] = newState;
    238.  
    239.                     //We are done, exit loop
    240.                     break;
    241.                 }
    242.  
    243.             }
    244.  
    245.         }
    246.  
    247.         //Update TimestampCount
    248.         m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
    249.  
    250.     }
    251. }
     
  2. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    did you find a solution?
    to me it happens when:
    - start an host
    - stop it
    - connect to another host
    - disconnect
    - start again the host
    - stop it
    and the error shows up in loop!
     
    Last edited: Jul 16, 2015
  3. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Hi tribio,
    It is quite strange.
    You have two options here:
    (1) report the bug and wait
    (2) publish your project here (only necessary for reproducing part) and I will take a quick look on it. But, If it is our bug I will ask you report them anyway :)
     
  4. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    Most likely it's my bug, as you can see I'm just messing around with the code,
    It's just a few days that I'm working on multiplayer in unity 5 for the first time,
    I hope the code could help you to find out the problem, maybe it's that I destroy the gameObject when disconnecting?
    But I can create n times the host and stop it, and connect to a host the same, the problem is closing the host after I connected to a host, and previously started an host!

    Thank you for your help!
     

    Attached Files:

  5. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    I get this error spammed as well on Unity 5.1.2, even when all I have is a "Network Manager" and "Network Manager HUD" in game, running on a Surface Pro 3 256gb Core i5 and Windows 8.1.
     
  6. mTr

    mTr

    Joined:
    Aug 6, 2013
    Posts:
    7
    I got this error too, on all my projects, even with Unity Network Lobby Example. The games works fine, but when quitting as server, this error got spammed and returning to "Main Menu" gets ignored and the game simply crash or freezes.

    The problem is that I can't find where to start digging to solve it. Sure, it comes from networking library, but where ? Also, I'm not sure if we can replace the original UnityEngine.Networking.dll but seems kind of hard.

    Btw, its a bit weird there's only 1 post on all the internet about this... This gives me a clue about a compatibility issue... Let's try to solve this, guys :D
     
  7. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    At least it doesn't crash on my android project!
    I'm lucky! ehehehehhe!
     
  8. mTr

    mTr

    Joined:
    Aug 6, 2013
    Posts:
    7
    Yeah heheh you're lucky, I'm with a pc project and cannot get back to main menu when disconnecting as server, host, or client... maybe I should try to override some disconnection handlers and try to do it by myself, but not sure if this approach will be safe...
     
  9. mTr

    mTr

    Joined:
    Aug 6, 2013
    Posts:
    7
    I think I fixed it somehow... or at least I made a little patch to avoid the annoying error. I just call NetworkTransport.Shutdown() then NetworkTransport.Init() after my lobbyManager.StopHost()... not sure if re-initializing the NetworkTransport can be expensive, but for now I'm gonna keep it that way till some fix comes out.
     
  10. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Hi @tribio just have found the time to take a look on your code/

    On the first look it looks ok. To do further investigation I need whole project and description how I can reproduce this issue. Report bug please, attach project and reproducing steps to this bug, and ping me after that with bug number...
     
  11. mTr

    mTr

    Joined:
    Aug 6, 2013
    Posts:
    7
    @aabramychev I can easily reproduce the bug with an unmodified Unity Network Lobby Example (https://www.assetstore.unity3d.com/en/#!/content/41836) only by clicking "Play and Host" then pressing "Back". The message is spammed 30 times before the editor pauses it.

    There should be some compatibility issues because it seems it works fine for the majority of devs. Or even a little misconfiguration we didn't saw.

    EDIT:
    Found this on the issue tracker (http://issuetracker.unity3d.com/iss...en-already-deleted-only-when-profiler-is-open) and deactivating profiler fixed it for me.
     
    Last edited: Jul 22, 2015
  12. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    @mTr But, I still have this error showing up with logcat on andorid...
    sorry @aabramychev but I can't share the whole project
     
  13. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    For me it didn't work... :-(
     
  14. mTr

    mTr

    Joined:
    Aug 6, 2013
    Posts:
    7
    @tribio I'm thinking that the error may come from the networking logs itself. I'm not sure but I got it solved by only closing the profiler. Have you tried to change the Log Level on the NetworkManager? I'm using Debug. Also, have you checked on the issue tracker (http://issuetracker.unity3d.com/) for the same error ? I found my solution there, so definitly worth it.

    It worked for me with Shutdown() then Init() before knowing the profiler fix. The problem was that it created another NetworkLobbyManager each time and had to destroy the old one. Also I'm not sure but I think this approach may ignore/jump some network callbacks, so take care of it.

    My conclusion its that the High Level API isn't polished yet. And I'm already preparing myself to swich to the Low Level API. Kind of challenge for me, to be honest... I'm not experienced with online games (only a couple of test prototypes), and I don't like the part where I must code things that are already done, like the NetworkManager. But I like challenges and it seems like the best way till Unity devs fix it :D
     
  15. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @mTr
    thank you. much appreciated for your investigation. Will try to fix this bug shortly.@tribio could you check if your profiler was opened?
     
  16. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    I'm trying it right now.. with the profiler opened as I stop the host I get the errors, instead if the profiler is close
    I still get the errors only if I start the host, stop it, join a match, stop the client, start again the host then close it.
    I'm not using a network lobby...
    Tomorrow I may be able to make a demo-project to share!
     
  17. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    thanks @tribio
    it will be beautiful.
     
  18. tribio

    tribio

    Joined:
    Oct 5, 2013
    Posts:
    29
    Guys, I don't have that error anymore!
    Sorry but I don't even know what I changed in my code.. I've been updating it all day...and now it works!
    Don't know what to say... the only thing I can say it's I added some NetworkMessages, before I didn't have them at all.. don't know if it could be related.. sorry not much time to dig deeper into this...
     
  19. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Hm :) ok, will wait for... (I remember about profiler)
     
  20. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    This error happens to me also on both 5.1.2 and 5.1.3
    I do hope this gets fixed, because i want to try UNet as fast as possible! :D
     
  21. bioert

    bioert

    Joined:
    Nov 9, 2014
    Posts:
    8
    Hi, this also happen to me. I had the profiler "open" in a tab. I closed it and it worked again. Thanks @mTr !
     
  22. Tiago-CC

    Tiago-CC

    Joined:
    Oct 30, 2014
    Posts:
    7
  23. Deleted User

    Deleted User

    Guest

    I got this error, when i disconnects from my master server, too.
    The reason whas that i didn't shutdown my NetworkClient correctly.

    I had this code to disconnect from the masterserver before:

    Code (CSharp):
    1. m_client.Disconnect();
    2. m_client = null;
    And now, i changed it to:
    Code (CSharp):
    1. m_client.Disconnect();
    2. m_client.Shutdown();
    3. m_client = null;
    And i got no errors more. Maybe this helps someone :)
     
    GunLengend likes this.
  24. Lnoldori

    Lnoldori

    Joined:
    Jan 19, 2013
    Posts:
    9
    For me it was only a matter of closing the profiler tab. I still do not know what causes this
     
    Oxygeniium and bitbiome_llc like this.
  25. bitbiome_llc

    bitbiome_llc

    Joined:
    Aug 3, 2015
    Posts:
    58
    Closing the profiler tab removed the errors for me as well.
     
  26. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    35
    Just confirming... I stopped my host. Then started getting spammed by that error. I opened the profiler, and closed it and the errors stopped....
     
  27. woodsynzl

    woodsynzl

    Joined:
    Apr 8, 2015
    Posts:
    156
    If you can, just disable your NetworkManager from not destroying on load
     
  28. Alturis2

    Alturis2

    Joined:
    Dec 4, 2013
    Posts:
    38
    Closing the Profiler tab fixed this issue for me as well.
     
  29. bzor

    bzor

    Joined:
    May 28, 2013
    Posts:
    35
    getting this as well, after calling NetworkDiscovery.StopBroadcast();
    closing the Profiler fixes the issue.
     
  30. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    755
    Has anyone ever figured this out? My console gets spammed with these messages if I open the profiler, which makes profiling difficult.
     
  31. reyou

    reyou

    Joined:
    Apr 30, 2015
    Posts:
    6
    After I turn off Profiles from build settings, it does not spam this weird error message. But gotta fixed from Unity side though.
     
  32. GunLengend

    GunLengend

    Joined:
    Sep 24, 2014
    Posts:
    54
    For someone still using UNET todo your own stuff, it's the right way to close connection and reconnect to the new one.
     
  33. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I'm getting this on the host, not the client. Quite annoying, happens only once in a while when a client disconnect.