I just tried to copy/paste a script from the Unity Website – specifically a script for quitting the game when the player presses Escape
. But then it didn’t work… what went wrong? Here’s the process I went through to discover and correct the issue:
- I created a new script called “GameController”
-
I pasted in the code from the website:
- but for some reason, it didn’t work.
- I tried removing and re-adding the script, and got this error:
-
After a bit of poking, I disovered it was this line that got me in trouble:
[csharp]
public class ExampleClass : MonoBehaviour {
[/csharp]
What’s wrong with this line? The filename was GameController.cs
but the class name was ExampleClass
. So, how do we fix it? Either rename your file to ExampleClass.cs
or modify the code to match the name of the script (which is what I recommend):
[csharp]
public class GameController : MonoBehaviour {
[/csharp]
This will also sometimes happen when you rename a script file, because Unity isn’t smart enough to refactor (aka auto-update the names in) your source code.