What is a Unity GameObject, and How Do You Fit It Into Your Game?
Despite the difficult circumstances of last year, Unity remains the game engine of choice, with 61% of developers surveyed using it and the second most popular choice as a mobile ad network. Every month, there are 5B downloads of apps built in Unity. In addition, the volume of HD games (video games on PC, macOS, and other desktop platforms with usually high-definition graphics) increased by over 38% in 2020.
Right now, 2.8B monthly active end-users are engaging with content created or operated by Unity solutions in 2020. Unity is a great platform that provides video game creators with many tools to bring their fantastic ideas to life.
What is a Unity GameObject?
GameObjects are essential objects in Unity that represent the basis on which characters, props, and scenery are built in the game development process. They do nothing on their own, but they are like containers to which the other components are attached, thus forming the object’s final functionality. Components are scripts that define the behavior of your GameObjects, how they move, position, rotation, size, and any other custom behavior. So, for example, the Light Object is born by adding the Light component to a GameObject.
The GameObject is the essential concept you need to become familiar with to start programming with Unity.
At the same time, it is widespread for Unity GameObjects, such as characters, treasures, to be created and then removed during the gameplay. This is something common, so just as a game developer must first learn to create a GameObject, he must also learn how to destroy it.
At its most basic, to remove a GameObject requires only two elements:
- A script that derives from MonoBehavior, Unity’s standard base class.
- A single line of code: ‘Destroy (insertGameObjectHere);’.
Supposing you have those two elements in the script, you can successfully destroy a GameObject.
To make things more straightforward in developing video games, game developers create an Instance of a GameObject. These are clones of prefabricated GameObjects and represent the basis of all in-game interactions in Unity. A prefab is created whenever they drag a GameObject from the Scene view into the Project view, saving it in their game’s Assets.
Basic characteristics of a Unity GameObject
Although GameObjects are the basic in Unity, they are still the most important things that can be added to a Unity scene. To better exemplify, we can take an “ObjectA” and go through the basic features of a GameObject in Unity. However, to find out more about controlling GameObjects, you can check scripts on the GameObject scripting reference page on the Unity website.
To represent the position in scenes, rotation, and orientation, a GameObject always has attached a Transform component, which cannot be removed.
Tags help identify types of GameObject in your scene and distinguishing between them. Also, you can perform some functions if the object has a particular Tag assigned to it.
Layers provide a similar way of including or excluding GameObjects from specific operations such as rendering or physics collisions. You can configure many cameras in the scene and make each camera capture one or more particular Layers.
This method allows obtaining the reference of a component of some specific type that is assigned to the GameObject.
If you want to enable or disable the GameObject in the hierarchy, the SetActive method allows you to do that. The result is equivalent to marking or unchecking the tilde at the top of the inspector when the object is selected.
How to use each of these basic characteristics of the GameObject in the development of the video game?
In the 3D space, the Transforms are edited in the X, Y, Z axes, and in the 2D space, only in the X and Y axes. In Unity, they are represented with red, green, and blue as in the images below.
Editing Transforms can be done in the Scene View or changing its properties in the Inspector. In the Scene View, you can use the Move, Rotate and Scale tools, which you can find in the upper left-hand corner of the Unity Editor.
To access the reference of the Transform component, you can use the dot operator as follows:
If you want to access the position of the GameObject in the scene, you use the operator dot again:
If you want to access the float that represents the component and the position of the object in the scene, you can do the following:
The same applies to the other parts of the Transform component.
In developing your game, you can use the Tag characteristic to assign to one or more GameObjects. For example, you can use the “Player” Tag to select player-controlled characters, and for those that players do not control, you can use the “Enemy” Tag. Also, for the armor and all the assets that the player must collect in the gameplay, you can use the “Collectable” Tag.
In order not to manually add GameObjects to a script’s exposed properties using drag and drop, you can successfully use Tag features.
You can also apply the GameObject.FindWithTag () function to find objects with the desired tag. Tags are helpful for triggers in Collider to find out whether the player is interacting with an enemy, a prop, or a collectable, for example.
A GameObject can only have one Tag assigned to it.
Unity includes some built-in Tags which do not appear in the Tag Manager:
To use Layers in your game development, the first step is to create a new layer that you can then assign to a GameObject. To do this, open the Tags and Layers window (main menu: Edit> Project Settings, then select the Tags and Layers category). After creating the Layer, you can assign it to several GameObjects, but keep in mind that each GameObject can only be assigned one Layer.
Using gameObject.GetComponent will return the first component that is found and the order is undefined. For example, let’s say that the “objectA” above has an AudioSource type component assigned to it, and we would like to access that component and adjust the volume. To do this, you can use this method:
This provides us the AudioSource reference assigned to it. If the GameObject has more than one such component, we can do the following:
To enable or disable a GameObject, you must refer to the given value of true or false. For example, if a parent is not active, the GameObject may be inactive. If this happens, accessing the SetActive method will not activate the GameObject, but only set the local state of the GameObject.
To activate it, you can use this method:
To deactivate it:
Although GameObjects are the basis of Unity, the process of creating and assigning components can be a little challenging, especially if you have no experience. If you need help developing your gaming app with Unity, our Starloopians are here, ready to share with you the knowledge gained in over ten years of experience.
Starloop Studios offers affordable, custom-tailored solutions that fit within the client’s timetables. So whether you need to turn your idea into a gripping game or speed up the time-to-market of your app, our experienced Unity developers will help you take full advantage of this highly functional platform. Feel free to contact us and schedule a free consultation.