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. Dismiss Notice

Question Does Unity recycle GetInstanceID in the same session?

Discussion in 'Editor & General Support' started by yosimba2000, Sep 15, 2022.

  1. yosimba2000

    yosimba2000

    Joined:
    Jun 3, 2021
    Posts:
    25
    For example, I spawn Gameobject A and it has InstanceID 123.

    I then destroy Gameobject A, and spawn Gameobject B.

    Is it possible for Gameobject B to have instanceID 123 now that Gameobject A no longer exists?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    I would start with the docs. Anything not promised should not be relied upon.
     
  3. yosimba2000

    yosimba2000

    Joined:
    Jun 3, 2021
    Posts:
    25
    The docs only mention that the InstanceIDs are guaranteed to be unique, nothing about if they are recycled when a GO is destroyed. Guess it's safer to assume they will be recycled, huh :)
     
    Kurt-Dekker likes this.
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,504
    It's implemented as an ever increasing unsigned integer, so they shouldn't be recycled, unless you run your game long enough that it overflows and starts from 0 again (which is possible on 32-bit platforms, but unlikely on 64-bit platforms).
     
    evyatron likes this.
  5. yosimba2000

    yosimba2000

    Joined:
    Jun 3, 2021
    Posts:
    25
    That's exactly what I was hoping for, thanks for confirming!