Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Attaching Unity Script to gameObject

Tags:

unity3d

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.

like image 726
Kamran Bigdely Avatar asked Feb 06 '19 21:02

Kamran Bigdely


People also ask

What is GameObject[] in Unity?

GameObjects are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the real functionality. For example, a Light object is created by attaching a Light component to a GameObject.

How do I add MonoBehaviour in Unity?

To create a MonoBehaviour you must use AddComponent on a GameObject. Further than that you cannot create a new GameObject at class scope. It must be done in a method once the game is running, an ideal place to do this would be in Awake.

What is class name in Unity?

Classes are the blueprints for your objects. Basically, in Unity, all of your scripts will begin with a class declaration. Unity automatically puts this in the script for you when you create a new C# script. This class shares the name as the script file that it is in.


2 Answers

To solve your issue:

  1. 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.

  2. 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).

  3. 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).

like image 70
Kamran Bigdely Avatar answered Sep 30 '22 16:09

Kamran Bigdely


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.

like image 42
RemiGc Avatar answered Sep 30 '22 17:09

RemiGc