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

Renamed a class/file, now "the name does not exist in the current context"

Discussion in 'Scripting' started by Dreamback, Jun 28, 2022.

  1. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    I had a Monobehavior class named Logger, and I started running into problems because apparently Unity has an internal class with the same name so other source files were referencing the wrong one. So I decided to rename both the class and the file to "UnityLogger". But now I get the error "The name UnityLogger does not exist in the current context" when trying to create a reference to it in another code file (in the same directory).

    IE,
    private UnityLogger _ulogger; gets that error, or trying UnityLogger.Instance, or whatever. It's just a regular public monobehavior class like what worked before, I just changed its name. There are no compile errors aside from the new context errors, no reason I can think of for the class to not be accessible. It's within an Assembly, but like I said, even other files within the same folder can't access it (and I did try to "Reimport" both the code files and the Assembly).
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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, which is stored ONLY in the metafile.

    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.
     
    TheDevloper likes this.
  3. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    Shouldn't the .meta file and GUID only affect references within Unity (scenes, scriptable objects, etc), not within other code files? I mean, Visual Studio shouldn't care about the GUID, but it's giving me the same errors
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    If Unity is happy then it's only VS having the issue.

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
    Bunny83 likes this.