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 Moving scripts

Discussion in 'Scripting' started by Fuzzypup, Sep 4, 2023.

  1. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    I notice that if you move scripts or change the name of a folder the object loses the script. It does not update the objects.

    I even tried creating a new folder, moving a single script. The object loses it asking for where it is.

    I think the word is detaches from the object.

    Any fix for this?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,006
    Do you see in the Inspector of the object a yellow warning sign with a message that reads, that there may be a script error or the script is missing?

    If you move the script within the editor, this should not happen.

    If you move it outside the editor (eg Explorer, Finder) and you don't move the .meta file along with it, then losing the script reference is possible.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,707
    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.
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    everything needs to know the asset path, to know where it is. If you change the path, without any chance of it updating that path, that path will get lost. Yup...

    An analogy would be you set your favorite toy on the kitchen counter last you remember, you mom sees it and places the toy in your room. When you go back to where you last knew its location, the kitchen counter, it's not there! Error! :confused:
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    It's fine to move it but move the .meta file with it as that describes the contents for tracking purposes. Don't just use the OS to move it without doing that because when you go back to Unity the old .meta will be deleted and a new one will be created which is bad.

    The same goes if you're renaming, rename the meta.

    Use the Project window to do this and Unity will take care of the details.
     
  6. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    Yea I am moving everything in the editor. I know if you move it outside the editor it does this. But it also does it inside.

    I tested it several times.

    If I have a script, I used visual studio to change the class name. Does not update. Calls it missing. If I put the name back it updates.

    If I move the script in the editor it does not update either. I have to remove the script and attach it back on the object. It reads as missing on the object.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    I might be wrong but Visual Studio doesn't support that. A MonoBehaviour class has to be named the same as its filename and along with a filename rename, the meta file has to be changed as well. Consider if what you're doing is an atomic operation i.e. class rename, class file rename and class meta file rename is happening. If not, it'll break.

    That may have changed in VS though because I use JetBrains Rider where refactorings outside the editor work perfectly.

    I've never encountered renaming inside the Editor causing problems though.
     
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohh yeah, doing it that way is a real pain. I've often found it much easier to make a new script named properly in Unity, then just copy/paste into the new script.

    If I remember correctly(doing the hard way), you have to rename it in Unity, then rename it in VS, also have to rename the Class name in the script, then click on properties of the file in the properties window of VS and fix it's file name and path(if changed) in that too..

    But as I mentioned, it's just much easier to make a new script, and just copy/paste the code over.
     
  9. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    840
    I've had no issue renaming files in VisualStudio *IF* I do it via refactoring the class name which also renames the file. But this only works because I have the Unity extensions in place and working as well. I'm not sure if renaming works in the project explorer since I basically never do it that way out of habit. Either way if you don't have the unity extensions in place and working properly (which can be trickier than it sounds) it won't work at all.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    If you rename it in Unity, the project will be regenerated so VS will see the update. If you do it in Rider then everything is updated.