Autocompletion not working in Visual studio

2019-01-01 03:28发布

问题:

I have a Little problem with scripts. When I\'m writing C# scripts for Unity with Visual studio it seems like I`m working in notepad, there are no autocompletion (correct?) and I must write it very slowly.

Please, watch the video. At start is the normal situation (from tutorial), second part shows my problem.

Video with problem

For example, the transform class does not show suggestion or autocomplete.

Image with Problem:

\"enter

Unity version: 5.5.2f1
Visual studio 2015
Visual Studio Tools for Unity is installed

回答1:

There is no auto-completion because the script says \"Miscellaneous Files\" instead of the of the name of the Project. Take a look at the image below that came from the video in your question:

\"enter

The \"Miscellaneous Files\" message can happen for many reasons:

1.It can happen when you open your Unity C# file from another folder instead of opening it from Unity Editor.

2.This can also happen because Unity crashed while Visual Studio is still open therefore corrupting some files.

3.It can happen because Unity was closed then re-opened but is no longer connected to Visual Studio. When Visual Studio is opened you get \"Miscellaneous Files\" and no auto-completion.

4.This can happen when Visual Studio Tools for unity is not installed.

5.When you create a script from Unity then quickly open it before Unity finish processing it or before the round icon animation stuff finish animating.


Most of the times, restarting Unity and Visual Studio should fix this.

I can\'t tell which one is causing the problem but I will cover the most likely solution to fix this.

Fix Part 1:

1.Download and Install Visual Studio Tools for unity from this link. Do this while Unity and Visual Studio are both closed.

2.From Unity Editor, go to Edit --> Preferences... --> External Tools. On the External Script Editor drop down menu, change that to Visual Studio 2015.

\"enter


Fix Part 2:

If newly created C# files are coming up as Miscellaneous then follow the instruction below:

1.From Visual Studio, go to Tools --> Options... --> Tools for Unity --> Miscellaneous. Under Show connectivity icon, set it to true then restart Visual Studio.

\"enter

2.When you re-start, connection icon should now be available in Visual Studio. Click it then choose the Unity instance to connect to. The red \'x\' icon should now turn into a brown checkmark icon. Now, when you create a new C# file in Unity, it should open without saying Miscellaneous.

\"enter


Fix Part 3:

Still not fixed?

Re-import project then open C# Project.

1.Close Visual Studio.

2.From Unity, re-import project by going to Assets --> Reimport All.

\"enter

3.Now, open the project in Visual Studio by going to Assets --> Open C# Project. This will reload the project and fix possible solution file problems.

\"enter


Fix Part 4:

Still not fixed?

Fix each C# file individually.

1.Click on Show All Files icon.

\"enter

2.Select the script that doesn\'t do auto-complete then right-click and select Include In Project.

\"enter


Fix Part 5:

Not fixed yet?

Credit goes to chrisvarnz for this particular solution which seems to have worked for multiple people.

1.Close Visual Studio

2.Go your project directory and delete all the generated Visual Studio files.

These are the files extensions to delete:

  • .csproj
  • .user
  • .sln

Example:

Let\'s say that the name of your Project is called \"Target Shoot\", these are what the files to delete should look like:

  • Target Shoot.csproj
  • Target Shoot.Editor.csproj
  • Target Shoot.Editor.csproj.user
  • Target Shoot.Player.csproj
  • Target Shoot.Player.csproj.user
  • Target Shoot.sln

Do not delete anything else.

3.Double click on the script again from Unity which should generate new Visual Studio file then open Visual Studio. This may solve your problem.


Fix Part 6:

If not working, check if you are having this error:

The \"GetReferenceNearestTargetFrameworkTask\" task was not found

1.Install Nuget PackageManager from here.

2.Restart Visual Visual.

See this answer for more information.



回答2:

I found an another way to fix this issue in a more convenient manner:

  1. Select the broken file in Solution Explorer.
  2. Open its Properties.
  3. Switch field \"Build Action\" from \"Compile\" to \"None\".
  4. Then switch it back to \"Compile\".

This will kill the synchronization between Unity and Visual Studio somehow.

The next time Visual Studio will reload the project, it will prompt a warning. Just click on \"Discard\".



回答3:

Unload and reload the project, in Visual Studio:

  • right click your project in Solution Explorer
  • select Unload Project
  • select Reload Project

Fixed!

I found this solution to work the best (easiest), having run into the problem multiple times.

Source: https://alexdunn.org/2017/04/26/xamarin-tips-fixing-the-highlighting-drop-in-your-xamarin-android-projects/



回答4:

  1. Select project in Visual Studio
  2. Click \"Refresh\" button

\"enter



回答5:

I hit the same issues today using Visual Studio 2017 15.4.5 with Unity 2017.

I was able to fix the issue by right clicking on the project in Visual Studio and changing the target framework from 3.5 to 4.5.

Hope this helps anyone else in a similar scenario.



回答6:

Two Alternative Options:

Fix 1

@singleton pointed me in this direction. Instead of changing the target in Visual Studio you should change it in Unity since the project is auto-generated.

First delete the auto generated Visual Studio files: .csproj .user .sln

Then from within Unity go to PlayerSettings and under \'Other Settings\' change the \'Scripting Runtime Version\' from Stable 3.5 to Experimental 4.6.

However, that didn\'t fix it for me.

Fix 2

I noticed all of the references to Unity related code was marked with a yellow warning. Check your error logs and see if this is the case. In particular see if you get the following error: getreferenceNearestTargetframeworkTask

If so try: Start Visual Studio Installer again.

On the Build Tools 2017, click Modify,

Ensure that \"Nuget targets and build tasks\" are ticked. This should become ticked if you click on Universal Windows Platform development.



回答7:

i found my solution by creating the .cs file from visual studio itself instead of unity editor

  • right click on project folder in solution explorer
  • add > new item
  • type \"unity\" on the search field on the top right
  • select \"CSharp MonoBehaviour\"
  • name your script on the bottom and click Add


回答8:

In my case, correct .net version was not installed on my PC. I install the .net 3.5 on my pc and that worked for me.



回答9:

For Windows or macOS:

Download/Install the Visual Studio IDE (with Unity Tools)

When installing, make sure you include installation of

Game development with Unity

\"enter

Then using Unity (you can double click one of your C# files), open a new C# project and the Visual Studio IDE should open with your new project structure.

\"enter

From there, you should be able to see what you are looking for.

For example:

\"enter

\"enter

For Linux (suggestion):

Try Monodevelop - Additional Information, it provides code completion/hints.



回答10:

I solved to install the same version of .NET on WIN that was configured in my Unity project. (Player Settings)



回答11:

My autocomplete also didn\'t work because Visual Studio Tools for Unity wasn\'t installed. So, after you install that, delete the auto generated Visual Studio files. Others said that you open file again and the problem is solved but it\'s not.

The trick is: instead of normally double-clicking the file, you need to open the C# file from Unity by right click and then \"Open C# Project\".



回答12:

None of the above solutions worked for me. However I opened the ProjectName.CSPROJ file and manually added the new file and it worked like charm



回答13:

The issue I faced was that the C# Project was targeting a different .NET Framework (4.7.2), whereas the Unity project had a different target (.NET 3.5).

I fixed this by changing the target in Unity as-

File -> Build Settings -> Player Settings -> Other Settings -> API Compatibility Level : Set it to the .NET version you already have installed (Check your .NET Version here). In my case, it was 4.x

After this, Visual Studio worked perfectly and autocorrect was fixed too.



回答14:

  • Go to Options on the Tools menu and then select Documents in the Environment node. (If Documents does not appear in the list, select Show all settings in the Options dialog box.)
  • Put a tick on \"Miscellaneous files in Solution Explorer\" and Click OK. (This option displays the \"Miscellaneous Files\" node in Solution Explorer. Miscellaneous files are files that are not associated with a project or solution but can appear in Solution Explorer for your convenience if you tick this option.)
  • Locate your file in the Solution Explorer under \"Miscellaneous Files\". Then drag and drop your file to where it should belong and voila! This will copy the file to where you drop it. You may now safely delete the older file under Miscellaneous Files folder if you wish to do so

Credits: https://stackoverflow.com/a/47662523/10471480


In case Scripts folder is not visible:

  • Click on \"Show all files\" in Solution Explorer
  • Locate the Scripts folder.
  • Right Click on Scripts and select \"Include in Project\"