Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug 9th and 10th elements in project list get swapped if they have identical filenames

Discussion in 'Editor & General Support' started by PolyMad, Oct 30, 2023.

  1. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Maybe I'm becoming dumb but I can't find if there's some error on my part here.
    More than testing by changing names, I don't know what to do.
    All works correctly before and after lines 9 and 10.
    I only wonder how I didn't ever notice this before.
    upload_2023-10-30_12-15-27.png upload_2023-10-30_12-16-24.png upload_2023-10-30_12-17-14.png
     

    Attached Files:

  2. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    It's not even like this about rows 9 and 10.

    upload_2023-11-4_20-4-21.png

    Am I stupid and not seeing something blatant or is there something going on here?
    This might be something stupid that I can't see, or might be a harmless bug or it could be something that lies there harmless until it destroys your project.
    Nobody interested?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,322
    Not really. To Unity's internal connections, filenames are irrelevant.

    Their order in the project browser is even less than irrelevant! :)

    Nothing about the filename and nothing about the asset type or even the asset itself connects anything.

    EVERYTHING in Unity is connected via its internally-assigned GUID.

    SCRIPTS OR ASSETS ARE MISSING OR BLANK

    ALWAYS move all Unity assets by using the Unity Editor. Any other way risks disconnecting the GUID in the meta file from the original file, and this will result in all connections to that asset being broken.

    Some info about Missing script warnings, broken prefabs, GUIDs, renaming GUIDs, etc:

    https://forum.unity.com/threads/problem-with-git-and-missing-scripts.1090876/#post-7024801
    https://forum.unity.com/threads/scr...ead-after-loading-editor.998413/#post-6487297
    https://forum.unity.com/threads/scr...ead-after-loading-editor.998413/#post-6488230

    EVERYTHING in Unity is connected to the above GUID, which is stored ONLY in the metafile, and hence why the metafiles ALWAYS MUST be source-controlled.

    When Renaming: It is super-easy to inadvertently change the GUID by renaming outside of Unity. Don't do that. Instead:

    - close Visual Studio (important!)
    - rename the file(s) in Unity
    - in Unity do Assets -> Open C# Project to reopen Visual Studio
    - now rename the actual classes, and MAKE SURE THE FILE NAMES DO NOT CHANGE!

    If you are NOT using source control while you do this, renaming files is an EXTREMELY dangerous process. Use source control at all times so that you can trivially revert if you miss a critical step and damage your project.

    If I had to speculate on order, it might be how it lies on disk, how it was first serialized, how its metafile sorted into the internal database, etc. But either way, doesn't really matter except to the OCD folks among us.
     
  4. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Thank you for the exhaustive reply, I always use Unity to do any file operation in the project, so to not lose any connection.

    I'm still curious about this bug, which seems harmless, being clearly only related to how Unity sorts the list, at this point.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,322
    It's only a bug if it violates a documented specification, or obviously otherwise causes a crash.

    I don't know of anywhere that says what order identical-named files will be presented in. :)

    For instance, every filesystem seems to sort things like this differently:

    1
    12
    0123
    123
    1245
    1 2 3
    1 23
    200
    200abc
    200 abc
    200-123


    Annoying? Yeah. Bug? No.
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,211
    Have you checked whether you notice this same sort order in your OS's file system? I was recently getting annoyed that Unity's Project view seemed to sort things in a silly way:

    upload_2023-11-5_10-30-22.png

    It seemed weird that folders and materials didn't sort in the same order. But just as I was concluding it was a bug, I found that Windows sorts the files identically, because windows is sorting based on the file extension (and the "." in it) being part of the file name.

    upload_2023-11-5_10-31-35.png

    In short, I think Unity's just relying on the same order applied by your operating system's file system. So, I'd go look on your disk and see whether your OS is also sorting things in this way, if you order the files by name.
     
  7. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    This chunk of screenshot comes from a brand new project, I am absolutely sure that I did all file operations inside Unity.
    I only copied the textures in the folder in Unity, I only had one material in there and duplicated it inside Unity.
    There were only the source files, all the .meta were deleted.

    upload_2023-11-6_4-24-34.png

    And this is in Windows:

    upload_2023-11-6_4-25-10.png

    As you can see Windows is sorting everything correctly.
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,539
    The technical term you're looking for here is "the sort algorithm is unstable," as opposed to "stable." If the keys are the same, do they remain in the same order they were originally found in the list or not.

    In your case, the keys are the same (the filenames) but they do not remain in the same order you expect. Interestingly enough, this could be due to the Unity sort algorithm itself, OR, the operating system returning the results of a wildcard file search in a stable ordering or not.
     
  9. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    I see.
    Would be really weird if Win would sort one way and give data another way though, but with MS everything is possible.