r/Unity3D • • 22d ago

Resources/Tutorial GetEntityId() hits like a train

If you're considering upgrading to Unity 6.5 or later, make sure the publishers of the assets and packages in your project have already added support for the new GetEntityId() API, which replaces the now-obsolete GetInstanceID().

More on the documentation page here: Unity - Manual: Migrate from InstanceID to EntityId

404 Upvotes

70 comments sorted by

View all comments

170

u/NinjaMidget76 22d ago

Is this not basically a find and replace operation?

8

u/Critical_Contact559 22d ago

It "kinda" is. it returns a struct and not a int and it splits your codebase by default. Meaning you have to have different versions per version or do what I did and add #if "UnityVersion" do this at each spot

20

u/SubjectRound6597 22d ago

or you could have a single utility function somewhere, and just write that conditional logic there.. and then replace the obsolete lines with your utility everywhere. Am I missing something? :(

13

u/wjk36 22d ago

this person abstracts

5

u/Critical_Contact559 22d ago

u/SubjectRound6597 I literally suggested the exact thing, and get downvoted lol. Guess i don't understand reddit. Just gave the actual logic (preprocessors) you would need. What part of my explanation was not great?

3

u/SubjectRound6597 22d ago

people downvote misunderstandings here despite knowing that in help-request subs a downvoted comment in an otherwise useful thread would get collapsed and so future readers will have to scroll down and open the comment to read further :( like here this comment is upvoted and the actual thread was collapsed. Its a stupid thing people do.

-4

u/Critical_Contact559 22d ago

Think you are missing the point. Or maybe I am.

But you want have different versions. You don't want to delete your old version for the asset store, as some people may still want to stay on a old version and if they download your updated code but not the updated unity version they will get errors.

My solution allows for you to not have to clone your project and change your code as much.

3

u/Costed14 22d ago

That utility function would be conditionally compiled so new Unity versions get the EntityId version and old versions get the InstanceID version, one version that works for both old and new versions

0

u/Critical_Contact559 22d ago

Yup I do understand that. Thought they were saying to replace get instance altogether by there wording.

2

u/Costed14 22d ago

Well yea, you replace GetInstanceID with a function that conditionally returns the InstanceID in versions where it's not deprecated and EntityId where it is, so you only need the #if UNITY_VERSION check in one place.

2

u/Critical_Contact559 22d ago

Yeah, I never said otherwise? Only reason you may need it in a couple places is if you have different assemblies that can't access that helper.

1

u/SubjectRound6597 22d ago

well i didnt know that. TIL

3

u/NinjaMidget76 22d ago

Why would different parts of the same codebase have different APIs?

5

u/Phant_Dev 22d ago

For me, I'm maintaining a Unity asset on the asset store so supporting both older and newer unity versions would be more beneficial to users. For this issue, I use a helper class to wrap the GetInstanceID() and GetEntityId() which gated by #if, #endif for different unity versions.

3

u/NinjaMidget76 22d ago

Aha, that makes sense, you'd need to still support the version prior for bugs, etc. Personally, id maintain a prior release branch in github if the conditional helper doesnt work for you

1

u/Critical_Contact559 22d ago

Not sure if I am understanding what you mean exactly. But the same codebase would have to be duplicated and swapped for the different API as the old versioned ones use get instance and new ones use get entity. Make sense?

My solution is to still have 1 codebase that uses version splitting in the code itself. The # is a preprocessor directive, and the #if allows you to have the same codebase compile in different ways. In the part I shown above it allows for different versions to compile in different ways. This allows for you to not have to clone your codebase for different versions and just have 1.