3

I want to display the number of coins on the screen, but I have error into my VScode:

The type or namespace name "UI" does not exist in the namespace "UnityEngine"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Main : MonoBehaviour {
    public Player player;
    public Text coinText;
    public Image[] hearts;
    public Sprite isLife, noneLife;

    void Start() {

    }

    void Update() {
        coinText.text = player.getCoins().ToString();

        Lose();
    }
}

enter image description here

Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
Amstergo
  • 43
  • 1
  • 2
  • 6

5 Answers5

9

For Visual Studio Code (VSCode) not detecting UnityEngine.UI, you can follow these steps to solve possible issues.

  1. In Unity, go to Window/Package Manager
  2. Search for "Visual Studio Code Editor"
  3. Downgrade to version 1.2.3 (or upgrade to 1.2.5, seems like 1.2.4 is the problem)
  4. In Window/Package Manager, Search for "Unity UI"
  5. Re-install the package
  6. In Edit/Preferences/External Tools, check the "Registry packages" option
  7. In Edit/Preferences/External Tools, click the "Regenerate project files" button"

If that didn't fix the problem, downgrade "Visual Studio Code Editor" to 1.1.3 instead.
Then you have to manually delete .csproj and .sln files at the root of your project and restart Unity.

Saleh Hosseini
  • 482
  • 4
  • 11
3

From this unity thread:

  • 1) Close Unity and any code editors you have open
  • 2) Make a backup of the Library folder from your project folder to your desktop
  • 3) Delete the Library folder from your project folder
  • 4) Open your project (Unity will rebuild the Library folder)
  • 5) Close Unity
  • 6) Delete the newly generated Library folder
  • 7) Move the Library folder (you backed up earlier) from your desktop back into your project folder
  • 8) Open your project
  • 9) Open one of your scripts UnityEngine.UI should be fine now
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
  • This worked, but broke autocomplete again for Visual Studio (which I could fix). Do you know how to help with this? – ballade4op52 Nov 02 '21 at 05:15
0

I had a same issue. I changed visual studio code editor version to 1.2.1 in Windows > Package Manager > Visual Studio Code Editor section; I hope it helps

0
  1. Switch to VS Community under External Script Editor.
  2. Regenerate project files
  3. Switch back to VS Code.
  4. DON'T regenerate project files.
  5. Restart everything.
AmyyazdaN
  • 303
  • 1
  • 9
0

Hi Bro Solved the problem, very simple. Delete Unity Hub.If not solved, download a new version of unity, don't run it with the hub. Then run it with the hub.