Brice - Hi and welcome to this video.
In this video we’re going to talking about unity’s component object model.
so you've already seen unity and played around with the editor a bit and you’ve seen that you game objects and components and you have an editor. So what does all this stuff mean, what does the inspector view mean? Well in unity objects, game objects are really simple things, they are almost empty. It’s like you have an empty template for something and these games objects have components and a very classic one is the transform component, almost every game object has a transform component that you can access that simple says where it is in the game space. And so other components, for example, are the renderer, something might be rendered in a certain way, sprite might have a sprite renderer component. And you can build your own components as well. for example scripts you create are components of game objects and that’s how you add them to game objects. And that’s actually what you see in the inspector, the inspector is simply the view of the components of the game object. And every component itself is the classical C sharp object, so you have the game object which is a unity entity which has components that are c sharp. And a great analogy for that is Lego. You have a construction which would be like your game object which has components which are like Lego blocks so each block is like a component and that makes up a whole.
Ben - so you may be inspecting here your paddle in your 3D pong game and it may have on a renderer component, it may have a transform component. It’s got all these components bolted together into the game object and when you go into the inspector you’re looking at all these different parts and seeing that they are all fitting together to make a coherent thing called a spaceship.
And the other thing that you may have come across is called a prefab, so what a prefab is like; imagine I’m the guy in the Lego factory and I’ve decided this looks cool, this game object, this thing I’ve made here that’s a spaceship, so I’m going to make a recipe for it. So if you were drag your game object down into your assets it would turn into a prefab and what that’s saying is it will encapsulate your recipe for building like this, it’s like the build instructions together. So that’s basically how Lego is made, they prototype it and then they write out how it’s made and that’s what a prefab is, it allows you to take it from your assets and say drop me a spaceship, drop me an enemy, whatever it is.
Brice – You’ll notice that in that model there are two blue blocks and that shows you can have two components of the same type for certain types of components specifically for scripts. So, for example, that spaceship could have two scripts component attached to it which would be the two different blue blocks. And if I wanted to get one particular component, let’s say I wanted to get that renderer component, I would use the get component method on the game object and that would look at the components that are currently attached and get me the component. So get component renderer please
Ben – so that is your renderer
Brice – give me this one. Now I want to get component, umm let’s say wings
Ben – ok, so there’s your wings
Brice – and get component chimney, is there a chimney on there?
Ben – there is no chimney, so I’m going to return null, I don’t have a chimney so null. Now by the way when it gets the components it’s not actually pulling the object apart, we’re pulling it apart for the example, but what you’re doing is getting a handle programmatically on that component so you can do things to do it. So you can take the wings, get component wings, you’re not taking them off there, you’re not pulling them apart, but you could then change their length or reconfigure them or change their colour or something else. You’re getting a handle on that part of your game object.
So that’s it really, that’s what you see over in the inspector, you see the game object and all of it components.
Brice – Thanks for listening and we’ll see you in the next video
Ben – Thank you