Home > Mobile >  How to fix issue where a Unity script is missing from Visual Studio but visible in Unity?
How to fix issue where a Unity script is missing from Visual Studio but visible in Unity?

Time:06-04

I have a problem regarding a Unity project I am currently working on. So far, everything went fine and I have been able to add 6 C# scripts to my project. However, I have now run into a problem. I can still create a new script ("Timer") in the Unity interface: View in Unity

However, this script does not show up in the script "hierarchy" (I am using VIsual Studio):

View in Visual Studio

I do not know why this is the case. The name of the script and of the class are the same. The script compiles without error. I can even attach the script to GameObjects and it does what it is supposed to do. However, other scripts don't have access to said script which I suppose is because the "Timer" script is not connected to the other scripts.

I would appreciate any help.

CodePudding user response:

Visual studio uses a so called "project" file to keep track of which classes / skripts are being used together. This project file (in this case it is the one named "Assembly-CSharp" is for Visual Studio only and is not the same as your Unity project! It seems like Unity didnt add your Skript to the C# project automatically, which it usually would.

To solve this you can manually add the Timer class to your C# project. Just right-click on "Assembly-CSharp", chose "Add" and then "Add Existing Object". You can then choose your Timer.cs from your Scripts folder and it will be re-added into the project!

CodePudding user response:

This is a common problem.

The fix is to:

  1. Close Visual Studio
  2. Delete all *.csproj and .sln files from your Unity project folder (the one containing Assets, Packages etc)
  3. In Unity, choose Edit > Preferences > External Tools
  4. Click Regenerate project files
  5. In the Unity Project window, double-click one of your scripts. Unity automatically regenerates the solution file along with all project files

Visual Studio opens showing your solution, your projects and scripts.

  • Related