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

Bug 2021.2. Swapping texture with same name makes it lose sprite references

Discussion in '2D' started by noisetree, Nov 6, 2021.

  1. noisetree

    noisetree

    Joined:
    Apr 10, 2015
    Posts:
    51
    Hi, previously I deleted texture/sprite sheet and then added new one with same name (sliced it) and all refrences like in animator stayed. Currently (2021.2.1) all references are lost doing the same thing.
     
    Deleted User likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,764
    References are done by GUID and sometimes a sub-number. If that remains the same, the link stays, otherwise it is gone. Names do not matter at all.

    It is possible Unity has changed how it identifies individual-sprites, but you should instantly see this if you are using source control and properly tracking your meta files for the image import.

    Some info about Missing script warnings, 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. It is super-easy to inadvertently change it 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.
     
  3. noisetree

    noisetree

    Joined:
    Apr 10, 2015
    Posts:
    51
    Thanks, I just described how it worked previously and now it doesn't, so i can't really update any texture/sprite sheet without losing all references doing stuff as i did in some cases before (i don't know relation of GUID with names, if such exists, but it worked only if i saved the same name). Maybe it's not a bug and some internal changes you mentioned
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Like @Kurt-Dekker wrote, Sprite data is stored inside the .meta file. If you are removing a texture inside Unity, the editor will also remove the meta file, and the Sprite data stored within it. If you are, however, removing the texture from the file explorer, and do not switch back to Unity before adding a new texture with the same name, the meta file should stay with its data intact. So it depends a bit on how you performed these actions.

    My general advice is to replace a texture rather than delete and then add a new copy, as this will allow you to keep the Sprite data.

    If you believe that this issue is a bug, please file a bug report, and we'll take a closer look at it.
     
    Kurt-Dekker likes this.
  5. noisetree

    noisetree

    Joined:
    Apr 10, 2015
    Posts:
    51
    Thanks for answer, yeah, replace is the best choice for sure.
     
    Ted_Wikman likes this.
  6. Deleted User

    Deleted User

    Guest

    There must have been a change from Unity 2018 to 2021 in terms of data on deleted assets. In 2018, you could have a sprite, with mode set to multiple, all sliced up and used for an animation. Then If you wanted to edit the animation, you could edit the png in whatever program, then delete the sprite (2018 must have kept the meta data), and add the new file with the same name and re-slice and it would fill back in the animation. 2021 doesn't do this which is a pain.

    How would I replace the texture as you described to where I don't have to re-do every animation?
     
    Last edited by a moderator: Apr 2, 2023
  7. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    When you say "delete the Sprite", I take it that you mean that you delete the image file. If Unity detects this change, it will remove the meta file and your previous data will be lost. It is better if you drag and drop/paste the new image file into the folder rather than deleting and moving a new file in, as this would prevent the meta file to accidentally get deleted.
     
    Deleted User likes this.
  8. Deleted User

    Deleted User

    Guest

    Yes I meant the image file. But just dragging a new file in doesn't fix the problem... it makes a copy of the file, and a copy of the meta. So regardless, you still have to go through every animation and re add the sprites.

    There is never an option to replace the current identical file which would solve the problem.

    And big thanks for popping back to this old thread offering help Ted_Wikman
     
    Last edited by a moderator: Apr 3, 2023
  9. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    If you drag a new file with the same name into Unity, Unity will add a " 1" behind the name, so it won't replace the file. It would be interesting if Unity gave you the option of replacing or adding a " 1" to the file. I can add that as feedback to the editor team.

    So what you have to do instead is to replace the file in your file explorer/finder. There you can choose if you want to duplicate the file or replace it.
     
    Deleted User likes this.
  10. Deleted User

    Deleted User

    Guest

    Sorry I should have said I tried both. When you do it in file explorer, or at least for me using Windows10, it goes "fileName - Copy. Same with meta. With no option to replace or copy or anything. Tried with Unity open and closed to see if that mattered.

    I did find out just now if I have the game's folder open in Assets, and another file of some images, I can delete just the image file in the folder and drag the same file (but edited image) from the images folder into the game folder, and it keeps the old meta.

    So I see what you are saying now. When deleting directly in unity it also deletes the meta, where as keeping the folder open, and not clicking in editor, you can replace the image file with same name file and all data is kept. Awesome, so that def works. Thanks for guiding me there.
     
    Ted_Wikman likes this.
  11. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Im happy you managed to resolve the issue. This feedback is also great for us to note down so that we can improve this flow in the future.
     
    Deleted User likes this.