- Home /
What is the concept of "object" class (not gameObject, just object) in Unity scripting?
I was reading through this : http://docs.unity3d.com/ScriptReference/Object.html and it seems that "objects" don't exactly work the same as "gameObject", in that it is never explicitly typed out like gameObject.
For example:
With gameObject, you can type something like: gameObject.camera
But with object, instead of typing Objects.FindObjectsOfType(HingeJoint), you just simply type FIndObjectsOfType(HingeJoint). Or instead of typing Object.Instantiate(...), you simply type Instantiate(...).
I also tried to explicitly type out objects in MonoDevelop and an error tells me: Unknow identifier 'objects'/'Objects'.
So I'm a bit confused... why is 'object' allowing for this kind of omission? Is it a concept that is different to gameObject?
Answer by AyAMrau · Sep 07, 2014 at 07:16 PM
Object is the base class of all objects unity can reference. If you look through the documentation for other elements, under the name you will see something like Namespace: Inherits from: [Class]. It may not always be Object but if you keep clicking the names you will (in not all but many cases) eventually arrive at Object.
The reason for this is simple, and what inheritance is in part for, it gives all the children a set of common variables and behaviours. Which allows the code to aggregate objects under a base type and use the common elements. In most cases you will probably not want to go as deep as Object.
GameObject inherits directly from Object and specialises it to represent objects that can be used in Unity scenes. Therefore it has access to (public and protected) members of Object.
But Object and GameObject are not equal.
Also within a MonoBehaviour you can use Instantiate or FindObjectsOfType without referring to anything, because MonoBehaviour inherits from Object (therefore you own script that inherits from MonoBehaviour also inherits from Object.
Answer by kacyesp · Sep 07, 2014 at 07:14 PM
GameObjects represent objects that are actually in the scene.
Object is the base type for all objects in Unity. An example of an Object that is not a GameObject is a script.
Here's another answer to this question: http://answers.unity3d.com/questions/707570/object-vs-gameobject.html
Answer by BMayne · Sep 07, 2014 at 07:14 PM
Hey There,
I am going to run over this very quickly :)
object (lowercase o ) is the base class for all .Net (C#) classes. It's the furtherest down you can go. Anything in .NET can be downcast to it.
Object (uppercase o) is Unity's base class. Everything you see in Unity project ie. Folders, Scenes, Scripts, sprites, music, etc are of base class Object. This becomes very important when doing editor scripting.
If you know about Polymorphism the base type Object becomes very useful for generic programming. If you don't know about it I would suggest reading up as it's a staple of OOP.
Regards,
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Help with hiding an object on trigger enter 1 Answer
Trouble grabbing and holding a cube. 0 Answers
Triangle Mesh Problem 2 Answers
help with game error 0 Answers