Unity Can't Find C# Script - unity3d

I can't seem to compile on Unity because all of my scripts have errors. The error messages keep on repeating, "The associated script cannot be loaded. Please fix any compile errors and assign a valid script." I have checked all of my scripts on Visual Studio 2017 and have not found any errors.
The image below shows the error showing on the inspector view of the unity engine:

If there are really no compiler errors as you say the reason might be incorrect file names or class types.
Make sure that the file names and the class names match!
If your class is called
public class Player_Collision1 : MonoBehaviour
{
...
}
the script/file must exactly be called
Player_Collision1.cs
and the other way round.
Attention: The Unity project view (Assets) strips of the file endings so there it should only display as
Player_Collision1
Another reason for a script to be not valid is e.g. if your class doesn't inherit from MonoBehaviour at all.
Unity would usually prevent you from adding those "invalid" scripts to an object but it might happen that you renamed them or changed their type afterwards. In this case you will see the error you currently have.

Check you log and see if any compiler error has happened.If no have a look at your class name and file name are same.

I see that you are (relatively) new here. welcome to Stack Overflow! ^^
what unity is trying to say is that it can't compile your scripts because there is one or more another script(s) that have errors in them.
in the Console, you will find your errors and by double left clicking on one of then, you will be taken to the script and the location of its error.
by selecting (left click) an error you will be able to read more a more detailed description of the error.

Related

Warning CS7022 - The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point

so I have an issue where I have this warning in my Error List:
Severity Code Description Project File Line Suppression State
Warning CS7022 The entry point of the program is global code; ignoring 'Program.Main(string[])' entry point. Project DirectoryToProject 23 Active
This is essentially where its throwing
namespace MyProgram
{
class Program
{
static async Task Main(string[] args) => await new Program.MainAsync();
}
static async Task MainAsync()
{.. do stuff.. }
}
That is the line of code that is causing the error. I've tried playing around with the Main class, I did have it with the return type void and had my GetAwaiter and GetResult method called on the MainAsync method.
I've tried researching the error but I've had no luck, so hopefully, this thread will help a few others...
I am currently running on C# 9.0
Visual Studio 2019 Build Version: 16.8.30717.126
EDIT: Forgot to show that the MainAsync was in the file... (Sorry) Im trying to limit the amount of methods I show as 95% of them aren't useful the to question... But the issue is that although my application compiles, when executing my program it quits instantly as if it doesn't know where to start...
EDIT 2:
Thanks to Hans Passant -
If anyone experiences something like this try what he mentioned:
"This is a rather awful C# v9 feature. Project > Properties > Build tab, Advanced button > Language version = 7.3 You should now get a decent error message from the code you didn't know you had to post".
Essentially upon changing back to C# 8.0 I saw it was a different file hidden away causing the issue.
Starting with net5.0, I've found that this error can be caused by having stray semicolons above the namespace keyword. Whether this is a bug or intended behavior is beyond me, however make sure you don't have any standalone semicolons as such:
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
; // This will cause CS7022
namespace Tomoe.Commands.Public
Be sure to check all your files and not just Program.cs
EDIT: Apparently this is intended behavior, see https://github.com/dotnet/roslyn/issues/53472.
TL;DR, semicolons above namespaces are interpreted as top level statements. Because nothing is being called in said statement, the program exits. This is the same as doing
static void Main() {
;
}
in your Program.cs. While I do feel some change should be made, the design decision behind this is quite logical and entirely understandable.
EDIT 2: According to jcouv on Github, this is now becoming an error instead of a warning. Hopefully, this "bug" shall harass us no more!
This can happen if a file (any file) in the project has global code, that's to say statements outside of a class.
As mentioned by others, this is caused by a new C# 9 feature that is called "Top-level statements". This Feature enables you to write statements in the global context and the compiler will create it's own Main() based on that.
In my case I had a semicolon after my using statements in any of my files. As far as I know Visual Studio or the compiler don't give you any option to find this "entry-point" without changing any settings as descripted by others in this thread.
My solution was to just create another "Top-level statement entry point" in my project. Due to the fact that there is only one allowed the compiler complains about that.
I just added a semicolon directly after the using statements in my Program.cs. Because this file is one of the first that are processed by the compiler any other file that contains a "Top-level statement" will cause an error.
I've also seen this compiler error in the following scenario. You've written your code with top-level statements. Later, you decide to absorb that logic into a Main() method. (Maybe you find you now need to return an async Task, or you need to modify it to meet a company coding standard, for example.) Even though the following code block will compile (in VS2022 at least), it generates the error in question with a green squiggly beneath Main:
static void Main()
{
Console.WriteLine("Inside the Main() method");
//Do some other work here
}
Where's the issue? The method declaration is correct, and it will run, but even when this is the only code in the Program.cs file, and even when no other entry point is specified in the project/solution settings, we do not get the expected output:
Even the Microsoft documentation isn't much help in this case, because it pretty much repeats in more detail what the error is saying.
What's missing is the Program class definition. Without it, the compiler is still looking for a top-level statement - which it finds, namely static void. Then the next thing it finds is the Main() method declaration, but it finds this after the (unintended) top-level statement static void. Hence, the error sorta makes sense now.
The fix is to wrap the above code in a Program class:
class Program
{
static void Main()
{
Console.WriteLine("Inside the Main() method");
}
}
And now we get the expected output:

Error in referencing function using package

NetBeans 8.2 Patch 2 (Build 201705191307)
My package hierarchy is:
spider;
spider.ui;
spider.ui.output;
My classes w/functions are:
spider.ui.DisplayManager.stateMachine
spider.ui.output.DisplayManager.stateMachine
The duplication of class names and function names is deliberate. All stateMachine functions are static, that is,
public static stateMachine() { }
I attempt to reference the spider.ui.output.DisplayManager.stateMachine in the spider.ui.DisplayManager.stateMachine using:
import spider.ui.output.DisplayManager;
stateMachine() {
spider.ui.output.DisplayManager.stateMachine()
}
and get a "ui" variable not found.
Cannot find symbol
symbol: variable ui
location: variable spider of type JFrame
I would have expected that if there was an error it would be in using duplicate names not in identifying the "ui" in spider.ui.output.DisplayManager.stateMachine() as being wrong.
It is not a great labor to change the names so that they are unique, but can anyone tell me why I get the error message I do?
I apologize and thank you all. This was most definitely an Operator ERROR, that is, it was created by me.
What was pointed out to me was that:
Spider spider; //variable and
o o o
spider.ui.*; // collide
Once "Spider spider;" was removed, the error was cleared.
What mystified me was that the error message said that 'ui' in "spider.ui." was at fault because "ui" was a variable which could not be found. If I had given it an even moment of thought I would have guessed that the compiler was treating "spider" as an variable name and not a package name. But mystified or not, the error went pfft once "Spider spider;" was removed.
Thanks again and I am sorry to have wasted your time.
The good news is that I earned some StackOverflow brouwnie points for giving a faulty analysis about an error. So I am just wonderful.
art

Why it compiles with different type?

Error:
IL2CPP error for type 'Namespace.SubNamespace.MyClass/<MyIEnumeratorFunc>d__20' in assembly 'Path\MyUnityProject\Temp\StagingArea\Data\Managed\VisualStudioSolutionName.dll'
Additional information: Interface System.Collections.Generic.IEnumerator`1<System.Object> method T System.Collections.Generic.IEnumerator`1<System.Object>::get_Current() not implemented on non-abstract class Namespace.SubNamespace.MyClass/<MyIEnumeratorFunc>d__20
Unity expect System.Collections.IEnumerator and I try to use IEnumerator:
I tried to write directly
private System.Collections.IEnumerator MyIEnumeratorFunc(){}
I tried to remove
using System.Collections.Generics;
and append it to each List/Dictionary entry
Everything works in Editor, but I get error I described above:
Seems like it tries to use System.Collections.GENERICS.IEnumerator.
Also, the error disappears if I use source code (not .dll)
Also, you can reproduce it by creating .dll with any IEnumerator/async function (even empty) and build for iOS.
The problem was in Project name.
Solution is
Right mouse click on Project name (not solution name) in Solution Explorer.
Properties
"Application" tab
Assembly name -> set to the same to .dll file name (without ".dll")

Error Attaching Unity Script to gameObject

I got an error when I try to attach my script to a gameObject.
'EnemyBehaviour' is missing the class attribute
'ExtensionOfNativeClass'!
Another times, I got:
Can't add script behaviour CallbackExecutor. The script needs to
derive from Monobehaviour!
I have made sure my class derives from MonoBehaviour.
In my case, this error was caused by a reference to an old version of a class that used to derive from MonoBehaviour(in current version, it does not). All that I had to do was to remove it from the GameObject on the scene.
To solve your issue:
Fix all compiler errors of this script and all other scripts. Compile
errors in other scripts can cause the Unity Editor not be able to
analyze your current script. Go to your code in visual studio and
build the entire solution. Fix all compiler errors before going back
to Unity Editor.
Make sure the class inside your script is inherited from
MonoBehaviour. This is the obvious one but surprisingly, sometimes,
you still get this message even if your class is inherited from
MonoBehaviour! If so, you should fix all compiler errors in other
scripts (Read item #1).
Make sure the class name is exactly the same as the script name
(even the same capitalization).
I solved mine by fixing a compiler error in a different script (item #1) and fixing a typo in my script file name (item #3).
If you are using latest version (2020+) those days you don't need to attach static class to the GameObject. You can call it from anywhere. So just delete script from object.

Eclipse debug can't find fields

I'm using the Eclipse debugger and can't inspect/watch field values or results of field.method() calls. I've encountered the problem in both Juno and now in Indigo. At first I could resolve the issue by wiping out my .metadata and rebuilding, but now the problem occurs even with a fresh build.
A specific error: I create a Deflater object deflater = new Deflater();,
set some input deflater.setInput(buffer, 0, bufferPosition);,
then try to inspect functionality by highlighting a section of code deflater.needsInput()
and doing a right-click->Inspect. The error reads: "Cannot find the field deflater for the object apps.TestCore$Tests (id=27)".
The error only occurs when the field belongs to an inner class (In this case "Tests"); when the variable is local or the class is not an inner class, everything seems to be working. Hovering over the variable "deflater" shows the contents drill-down just like it should. Highlighting "deflater" and doing an Inspect gives the error, and using the Expressions view to inspect the variable/call methods on the variable gives the same error.
Please help; this is making my debugging life very difficult, as I have to use println() for anything more complex than a hover inspection can provide.
This is not remote debugging - just local to my system.