2

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.

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I did this, and everytime I create a new csharp file I have to regenerate the project to be able to view it in Visual Studio.
@STEEL did you find a solution to always having to do this?
0

Visual studio uses a so called "project" file to keep track of which classes / scripts are being used together. This project file (in this case it's "Assembly-CSharp", where your self-made scripts are stored in) is what Visual Studio uses and it is not the same as your Unity project! It seems like Unity didn't add your Script to the C# project automatically, which it usually would.

(This could be a sign of a broken project, so I would recommend going with MickyD's solution instead of mine, as their solution is likely to prevent future incidents of this as well!)

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!

3 Comments

"'Assembly-CSharp' is for Visual Studio only" - well no, it's for "All other scripts that are not inside a folder called Editor." and not for scripts belonging to Assembly-CSharp-firstpass and Assembly-CSharp-Editor-firstpass and incidentally all of these Unity-created projects are still visible in the Visual Studio solution Unity also creates. docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
"To solve this you can manually add the Timer class to your C# project" - whilst that may work, Unity is the one that generates the project files in the first place. You are fixing a symptom not the cause
Thanks Jayne and also for the plug :)
0

I updated my Visual Studio and it fixed the problem. Fortuneatly I was behind in my updates and had one available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.