Hi there guys
I need to explain to someone the difference/relationship between the UnityEngine reference and inheriting from MonoBehaviour, but although I have been using unity for a long time I’m having a hard time putting it into words and using the correct terms. Can someone please give us a nice, short layout?]
Much appreciated!
UnityEngine.dll is what is called an assembly. Think of this as a storage container. Within the container there are several namespaces. Namespaces are like folders, they can be nested.
So… our assembly UnityEngine.dll contains a namespace called UnityEngine and UnityEngineInternal. MonoBehaviour is a class that resides in the UnityEngine namespace.
The fully qualified reference to the MonoBehaviour class is global::UnityEngine.MonoBehaviour. Since we like to be lazy, we can tell the compiler to implicitly add the ‘global::UnityEngine’ prefix by declaring, ‘using UnityEngine;’ at the top of our file.
Some finer points: Just because two classes are in the same namespace does not mean they are in the same assembly. This means, you can define a class like so:
namespace UnityEngine {
class MyClass {
// Despite being in the UnityEngine namespace, my code is not in the UnityEngine assembly and as such cannot see objects marked ‘internal’ in UnityEngine
}
}
Thanks Andorov, that really cleared it up.
Last question : Isn’t it a bit confusing to call the MonoBehaviour class “MonoBehaviour”? Why did Unity decide to call that class MonoBehaviour and not something closer related to the engine? What is the reasoning behind the name then?
I’m not 100% sure, but the conclusion I arrived was this…
There probably is an underlying Behaviour type that the unity team uses for internal behaviours (like colliders) thats called ‘Behaviour.’ The name MonoBehaviour name probably comes from the fact that the class is designed to be used with the Mono libraries (which is what C#/JS/Boo scripts are compiled with)
Ok, got it. i saw that link also, thanks