AssetDatabase.TryGetGUIDAndLocalFileIdentifier() returns false for no apparent reason

Hi everyone,

for a project where I want to work with Unity’s yaml files, I am trying to link the fileIds from the yaml to the objects on my scene. I tried

            GameObject[] gos = SceneManager.GetActiveScene().GetRootGameObjects();
            foreach (GameObject go in gos) {
                string guid;
                long file;
                if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(go, out guid, out file)) {
                    Debug.Log("Asset: " + go.name + " Instance ID: " + go.GetInstanceID() + "  GUID: " + guid + " File ID: " + file);
                }
            }

but for a reason I can’t think of, TryGetGUIDAndLocalFileIdentifier returns false for all the game objects while the game objects themselves are listed correctly. What did I overlook? I mean, it’s called TRY get GUID etc, but the documentation only says it will return false when it fails, not what could cause it to fail.

Thanks in advance. If you know of a better way to achieve what I need - a data structure where I can find objects in the scene based on the fileId from the scene’s yaml - I’ll take it, too. Ideally, this should work for components, too, I just started with the root game objects because that was easy.

I know of GlobalObjectId.GetGlobalObjectIdSlow() but all those methods are probably called Slow for a reason. There must be a working reasonable method, I can see all the things I need in the inspector…

I also know of UnityEditor.Unsupported.GetLocalIdentifierInFileForPersistentObject() but that only works for objects stored on disk (persisted objects) and also I bet it’s in a namespace called Unsupported for a reason.

You can use GetGlobalObjectIdsSlow and similar plural methods to get items in batches.

Thank you. Wow, that note should be in the docs of AssetDatabase.TryGetGUIDAndLocalFileIdentifier. I didn’t find it because in the docs for 2021.3 it’s not even on the page you linked.

I marked this post as solved because my question why TryGetGUIDAndLocalFileIdentifier fails was answered. However, the documentation for GlobalObjectId says “you should not rely on the value of targetObjectId, or the {l} element from the string representation of a GlobalObjectID, to find an object”, so using it is not a satisfying solution to the problem I was actually trying to solve.