I am working on a car driver game in Unity. I was attempting to make some touch screen buttons and adding a C# script for testing mouse and touch input, but I get the console error message
"Assets/Standard Assets/MouseButton.cs(4,28): error CS0246: The type
or namespace name `MonoBehavior' could not be found. Are you missing a using directive or an assembly reference?"
Script listed below:
using UnityEngine;
using System.Collections;
public class MouseButton : MonoBehavior {
void OnMouseDown() {
Debug.Log("The Mouse is down on " + this.name);
}
void OnMouseUp()
{
Debug.Log("The Mouse is up on " + this.name);
}
}
Small typo, but it happened to me too, so I can relate.
It's MonoBehaviour, as you would say in British English, not MonoBehavior.
https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html
For me on Windows 10, Visual Studio 2019, and Unity 2020LTS was working:
Check that is actually "MonoBehaviour" and not "MonoBehavior". Or an individual script behavior. If you open a second script MonoBehavior is not marked as red.
If the problem subsists in all scripts, then make a backup (just in case), and In Project right-click Asset and "Reimport All".
If that was not working, and the issue persists, select Edit > Preference... > External Tools > External Script Editor > and switch "Visual Studio.." to "Open by file extension" or vice-versa. Just switching was working. Attention: "Open by file extension" will work without auto-completion.
If you select "Open by file extension" auto-completion will not work. You can try "Visual Studio [version number]". Then go to Package Manager > Packages: In Project > Visual Studio Editor and upgrade to the latest verified version. Remove "Visual Studio Code Editor". And lastly "Reimport All".
Related
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Driver : MonoBehaviour
{
void Start()
{
}
void Update()
{
// transform.Rotate(0,0,0.1f);
transform.Translate(0,.01f,0);
}
}
above is my code. If I add anything incorrectly, no error appears. It appears to be a problem with omnisharp. I have tried reinstalling, fidgeting with the settings, and installing older versions of the C# extension. However, no matter what I change, errors do not appear as I code.
VS Code cannot automatically complete the code or check errors, and the plugin fails.
The following steps are helpful:
First make sure the plugins are installed.
List of plugins:
Debugger for Unity.
Unity Tools.
Unity Colde Snippets.
Make sure the development environment is complete.
An incomplete development environment can also cause this problem. It is also possible that the .net version created by the project does not match the .net version of the development environment.
Solution:
2.1. Use a text editor to open the file with the extension csproj in the root directory of the project.
2.2. Ctrl + F Search "TargetFrameworkVersion" keyword, you can see the information "v4.7.1". This means that your computer lacks the 4.7.1 version of .net Framework, and different computers default Versions may vary.
2.3. Microsoft official website to download the corresponding version. It should be noted that you need to download the develop pack (development package) version, not the normal version. Otherwise useless.
2.4. Restart vscode.
VS code editor settings.
3.1. Enter the vscode editor and press ctrl + shift + P at the same time.
3.2. Enter in the pop-up input box: OmniSharp: select project.
3.3. Manually select the project file.
Normally, vscode cannot complete the error check because the version of omnisharp is wrong, omnishap may conflict with other versions of your vs code during the upgrade process, so you need to keep omnisharp to the latest version.
Solution:
4.1. Go to the console. Then click on the ouput bar and change the output type to ominisharp log. Windows version shortcut key: ctrl + shift + u.
4.2. Search for ominisharp in the setting.json file of vs code and change its path parameter to latest.
{
"[csharp]": {
"editor.defaultFormatter": "bilal-arikan.csharp-auto-formatter"
},
"omnisharp.path": "latest"
}
4.3. Restart vs code.
Hope it helps you.
I'm writing the code but the editor doesn't detect it. I looked in many places but could not solve the problem."Rigidbody" does not turn blue. Thank you in advance for your help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KarakterKontrol : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Rigidbody fizik = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
}
}
I took a quick look at what "Diger dosyalar" meant.. which roughly translates to "Other Files", or in the English version of Visual Studio, Miscellaneous Files.
What that means is that your Visual Studio is not properly synced with Unity. You're seeing just a plain old C# file by itself, not a file that is associated with a Project.
The steps to rectify are usually:
Make sure "Visual Studio Editor" ( a.k.a. Visual Studio Tools for Unity) is installed in Package Manager.
Go to Edit > Preferences > External Tools and make sure that Visual Studio is set as your "External Script Editor".
Restart both Unity and Visual Studio, or even your whole machine to make sure.
9 times out of 10, that will resolve your issue.
I'm trying to handle in one time multiple projects in Visual Studio (SteamVR, Obi). Problem only occures in Visual Studio, in Unity Editor everything works fine.
My projects in Visual Studio 2019 look like this:
Now, I want to write code in main project script which will connect all projects. Code:
using Obi;
using UnityEngine;
using UnityEngine.Rendering;
using Valve.VR.InteractionSystem;
public class ObiURPSteamVR : MonoBehaviour
{
//1 - Obi - no problem in Unity, VS error in declaration
public ObiEmitter emitter;
//2 - URP Volume - working
public Volume URPVolume;
//3 - SteamVR - no problem in Unity, VS error in declaration AND in "using"
public Player steamVRPlayer;
}
My question is how to connect all projects, is it possible? And i will just add one more time, in Unity Editor everything is working, i can drag-drop SteamVR player and ObiEmitter, no errors are in console output. Thanks for every idea.
In Visual Studio, create an "empty solution". Add all your projects to this solution. When one project is referencing types from another project, right click on that project and "Add Reference".
So apparently its a bug in Unity 2019.3.12f version. Checked on 2019.4.01f and everything is working perfectly.
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();
}
}
For Visual Studio Code (VSCode) not detecting UnityEngine.UI, you can follow these steps to solve possible issues.
In Unity, go to Window/Package Manager
Search for "Visual Studio Code Editor"
Downgrade to version 1.2.3 (or upgrade to 1.2.5, seems like 1.2.4 is the problem)
In Window/Package Manager, Search for "Unity UI"
Re-install the package
In Edit/Preferences/External Tools, check the "Registry packages" option
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.
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
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
Switch to VS Community under External Script Editor.
Regenerate project files
Switch back to VS Code.
DON'T regenerate project files.
Restart everything.
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.
I'm using Visual Studio 2017 to edit the scripts for Unity 3D Project.
In a script, I want to create a new function "Move", but I get a problem with autocomplete.
If I write "public void Move"
I get a a proposal "OnAnimatorMove", but I don't need this function!
Now, after "Move" I enter a space character " ", but my function name is then automatically replaced by "OnAnimatorMove" and the "{","}" are automatically inserted.
Same happens, if I try to insert "(" character after "Move".
How can I prevent it?
U can click escape to disable the autocompletion or u can disble it all together by going to
VS > Tools > Options > Tools for unity > and under 'code edition' disable Unity massage code completion.
after that restart
I can solve it, going to Edit->Preferences->External Tools, and in External Script Editor, select Visual Studio 2017 (Enterprise)