In this video, learn about GameObjects in Unity. A GameObject represents any entity in our project's scene.
- [Instructor] Before we dig into scripting, we should take a moment to discuss what are GameObjects in Unity. A game object represents any entity inside of our scene. We've already taken a look at one game object and that's the main camera. As we add game objects to our scene, they'll show up in the hierarchy window. When you select a game object, the properties of that game object are displayed in the inspector. The inspector is also a good indication of the types of scripts attached to a particular game object.
All game objects share a similar hierarchy. They always start with a transformed script. As you can see, the main camera's transformed script defines the x, y, and z position, rotation, and scale of this game object. Below the transformed script is a camera script. This script makes the main camera unique from other game objects we would add to the scene. We could right-click on the name of this scene, go to the GameObject menu, and create additional game objects for our scene.
If we create an empty game object, it'll simply be called game object and include a transformed script. These scripts are called components. At the end of the inspector, for any of the game objects, we can add a new component by clicking on the button. Here you can search through all of the items available to a game object. Each of these components, or scripts, add additional functionality to a game object to make them work.
Let's go ahead and delete this game object. We can right-click on it and select delete. You can also create game objects at the top of the window from the GameObject menu. Let's go ahead and create a 2D game object called Sprite. A sprite represents any image that would be rendered in a scene. Sprites are used primarily in 2D games. As you can see, our new sprite has an additional component called the Sprite Renderer.
The Sprite Renderer allows us to display a texture which would represent the sprite in the scene. By default, no sprite is set. Let's go ahead and change this. Next to the input field for the sprite is a little circle. We can click on this to open up the Explorer and look for sprites in our project. By default, our asset folder contains several sprites that are part of Unity's new UI framework. Let's go ahead and select one of these as an example to show how a Sprite Renderer would work.
Let's select the UI Sprite. Double-click on it and now the Sprite Renderer should display a square with rounded corners. We can double-click on the game object itself in the hierarchy menu in order to zoom in and see it. Likewise in our scene, this sprite is going to be very far away, because this sprite is actually very small. We can increase the scale of our sprite or we can click on the main camera and change the size.
For number input fields, if you hover over the name of the field, you'll get a cursor with two arrows on either side. Simply click in this area and drag to the left or the right to change the value. Here we'll just zoom in so we can see the sprite a little bit better. Game objects are an important concept to understand in Unity. Not only will we continue to add game objects to our scene to build out our game, but we'll also attach scripts to our game objects to give them the functionality to work as our game intends them to.
- Creating a new project
- Using the IDE
- Creating C# scripts
- Creating GameObjects
- Creating public fields on your C# scripts
- Adding new scripts
- Talking to other scripts