GetInstanceId range of possible values

Hi,

I'm creating a script to export out scene data and I have a couple of questions regarding object ids.

  1. What is the maximum/minimum possible values for object ids accessed via Object#GetInstanceId?

  2. I've seen that the id value can be positive or negative - is there a practical difference?

  3. Also the values sometimes don't seem to be sequential (i.e. three cubes created at the same time have ids of -558, -11726 and -12668) - I was wondering why this was.

Cheers,

1 Answer

1

1 - What is the maximum/minimum possible values for object ids accessed via Object#GetInstanceId?

Since `Object.GetInstanceId` returns in `int` which is `System.Int32` in .NET perspective (CTS to be more specific), it's range is -2,147,483,648 to 2,147,483,647 or in other words -2^31 to (2^31 - 1).

2 - I've seen that the id value can be positive or negative - is there a practical difference?

Considering above fact, it probably makes no difference; also consider the following.

3 - Also the values sometimes don't seem to be sequential (i.e. three cubes created at the same time have ids of -558, -11726 and -12668) - I was wondering why this was.

The `id` kind of things are generally generated using hash values algorithms; so you can't guarantee what the next number might be!

Hope this is helpful enough :)

If you really need these constants you can refer to int.MinValue and int.MaxValue.

Wouldn't that cause problems when the hashing algorithm returns a value that is already used? If Unity checks the ID after generating the value, it seems to me that it computes more than necessary, starting with the minimum int(perhaps unsigned for aesthetical reasons) value and counting up sounds less computational invasive. Aside from that, why computing the hashing algorithm at all if counting up is way more efficient?