Check out a free preview of the full Game Development with Unity for Web Developers course

The "C# Custom Components" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:

Nick discusses using custom components such as a RollingMovement component to modify the behavior of the GameObjects in the Scene and the C# variables involved in the script. A student's question regarding if it is better to use the Unity input object instead of hard coding the key inputs is also covered in this segment.

Preview
Close

Transcript from the "C# Custom Components" Lesson

[00:00:00]
>> And now, we are going to write, our own custom components.So we added some built in components, that were already a part of unity or part of packages. But you can create your own components, and that is how you drive the behaviour of game objects. So this is where we're gonna start getting into code, the first component that we're gonna create is called rolling movement.

[00:00:28]
And I'm going to type all of this out in the editor, so don't worry about trying to copy all of this down. This is just here for reference, it's in the slides, if you need to go back and look at it. So, like I was saying earlier, components modify the behavior of game objects in the scene.

[00:00:49]
So we've already added a rigid body and a player movement, or sorry, player input component to the player game object. This component is going to take a 3D vector, in this variable called movement direction. And then we're going to apply that vector, as a force to the rigid body, to its physics component.

[00:01:19]
And that's going to roll the ball in the direction of that vector, and this vector is going to have a magnitude of one, so we can multiply it by another variable called speed. To move it at whatever speed we want to.We're gonna set the speed to one, and that's actually the speed that we're gonna use in the game.

[00:01:42]
So we don't actually need to multiply this vector, but it's something in the inspector that like I said, you can play with, you can play with the speed of the sphere to modify it. So we are going to code this up and just a moment.But there's more that I want to point out here because this is a lot of stuff, there's a that is being introduced here.

[00:02:07]
So first of all, the name of this class rolling movement, is also the name of the component. So when we create scripts in Unity, we named them the name of the class, but that's going to show up as the name of the component. So when we use Pascal case, with a capital R and a capital M, it's going to place spaces between the two words there, when it shows up in the inspector.

[00:02:37]
So it will say rolling space movement even though the name of the class has no spaces. All C sharp scripts that are components are public, so that they're accessible in the Unity editor, and they inherit from the class MonoBehaviour. And you'll notice that is not the American spelling of behaviour, it's because Unity is not an American company, so that is not a misspelling it is behaviour with a U.

[00:03:08]
So that's really important all C sharp scripts that are components inherit from the class MonoBehaviour. And let's break this down further in a different way, at the top, you'll see some of those namespaces, that I mentioned when we were talking about the documentation. Namespaces in C Sharp are basically just collections of code, there are a way to organize your code.

[00:03:35]
So that you don't confuse the names of your classes with say a class, and another namespace that might have the same name. There's also variables, so the variables I have listed out here and we're gonna break down what these variables actually mean in just a moment. And then below all the variables, there are C sharp methods, there's a few that are built into the MonoBehaviour class, such as start, update and fixed update.

[00:04:09]
And then, you can write your own methods, like this one that will reset the position back to the start.Do we have a question?
>> Is it better to use the Unity input object, instead of hard coding the key inputs and move inside the code?
>> Well, we're gonna do a little bit of both, we need to write some code that actually interacts with that set of actions.

[00:04:40]
So just adding the actions asset isn't going to do anything, when we create the actions asset. That's really just mapping concepts in our game to a set of controls. So well, in our case, if you have something moving around, you might wann map that to the WASD keys.

[00:05:06]
The arrow keys on a keyboard, but then on a gamepad you might wanna map that to a joystick. So it's a way of creating a concept within the game like, firing a laser or moving something around that's mapped to a different set of controls, it's an abstraction. However, we also have to write code that actually interacts with that action.

[00:05:30]
So when If that button is pressed on the keyboard, we need to have code that executes that actually does something with it. So we don't just get whatever those buttons are supposed to do for free, because it could be anything.Is that the only question? Yeah, cool, all right, so let's move on from here, So, we got a lot of variables here, there's a lot of stuff in the script, let's zoom in, and take a closer look at just one of these, so you can understand what each part of this means.

[00:06:12]
So if you are, say, a JavaScript developer or you're working in some other web programming language, a lot of this stuff should be familiar to you, a lot of it should map pretty well one to one. We have a type here, so in this case this variable, this is of type float, it's a floating point value it's a number.

[00:06:39]
We have a name for our variable m_ speed is what I've called this, and then we have a value. That I'm assigning to this by default, you don't have to assign a value to it, by default, any float or integer value is just going to be zero. But you'll see I have a little f there, after the number that indicates that it is a floating point value that's being assigned to this.

[00:07:10]
The m underscore necessary, but it's something that I like to add and Unity will actually automatically omit when this is shown in the inspector. That means that it is just a member variable, it's a member of this class. And it's again, not necessary it's syntactic sugar I would say, just so that you know what's a local variable, and what's a variable that's part of this class.

[00:07:38]
The thing that might not be familiar in JavaScript or other web programming languages, are what are called attributes. And quite frankly, maybe they are familiar, I'm not really a web programmer anymore, I'm very much into games. So maybe there's concepts that map to this, but an attribute, in C sharp changes the behavior of variables in a couple of interesting ways.

[00:08:07]
So I'll walk through attributes in just a second, but the last thing I wanna point out here is that, you can also add access modifier. So in the case of this where there is no access modifier, this variables private, so when there's no access modifier variables are private by default.

[00:08:30]
I'm going to go back just one slide just so you can look at some of these other variables that are here. Like this one has a different attribute hide an inspector, but then it does have an accessor of public there, has a different type of vector three, and then it has a different name for the variable.

[00:08:51]
This one has no attribute, no accessor, it just has a type and it has a name. And that's actually the minimum amount that you can have for a variable in Unity. You need at least a type, you really need at least a name, and then we also have our rigid body there just like that.

[00:09:10]
So I just wanted you to see those different things just so you can get the pattern of attribute accessor type, and name and then value. So let's talk more about those attributes, so, attributes are markers that can be placed above a class, a property, or even a method or function in a script to indicate special behaviour.

[00:09:40]
So they're placed in square brackets, and I think maybe the best way to explain attributes is to tell you about two of the attributes that we're using in the script. So serialized field forces Unity to serialize, a private variable, and show it in the inspector for modification, so you saw that we had that speed variable.

[00:10:05]
When we're working on the game, we may wanna change that value in the inspector different from what it says in the code. And serializing the variable allows it to be shown in the inspector, all public variables are serialized by default. So if you create a variable that is public, it will be shown in the inspector.

[00:10:28]
And basically, serialization is not something that you really need to worry about a whole Lot in Unity, but it happens behind the scene. But serialization Is the process of transforming data structures or game object states, into a format that Unity can store and reconstruct later. So it's just going back and forth between your code, and the Unity editor and allowing you to work with those C sharp variables in the inspector in a way that unity understands.

[00:10:57]
And to get a little bit more under the hood, Unity is actually written in C++ and C sharp is the scripting language that we're using on top of that. So that's why serialization occurs, it's going through a lot of different layers of software there that we don't need to worry about [LAUGH] That's the great thing about Unity.

[00:11:19]
There's another attribute that you saw in that script called hide and inspector, so like I said, all public variables are serialized. They're all visible in the inspector, but sometimes you want a variable to be public so that it's accessible by other scripts. But you don't want it to be shown in the inspector, because you don't want it to be modified when you're tuning the game.

[00:11:44]
So hide an inspector allows you to create a public variable, that is then hidden in the inspector. There's a couple of different methods in this script, there is start and fixed update, and those are two methods that are part of the MonoBehavior class. There are lots of other methods that are part of MonoBehavior, you can check that out in the documentation we're not going to go through all of them.

[00:12:15]
Cuz it would take all day, but start Is called on the frame, when a script is enabled, and I say frame as in like FPS frame rate of the game. So the first frame that's rendered, start gets called, and as an example when you hit the play button in the Unity editor, start is going to get called because it's rendering the first frame.

[00:12:39]
Okay, so that allows you an opportunity to kinda set up your scripts. You can gather other components on the same game object that you might need, you can store variables that you're going to need the values later. Fixed update Is a frame rate independent method, that is called every physics update.So there's a couple of different updates happening in Unity.

[00:13:07]
There's the physics update loop, there's what's called the actual update loop, where update is when a frame is rendered. But you don't want everything in your game running based on the frame rate, because the frame rate could be really high on like a high refresh rate monitor. Or maybe the game is having performance issues and it's running at like 15 frames per second.

[00:13:35]
There's things that you don't want running based on the frame rate, and one of those things is physics. You want physics to always behave in a consistent way, even if the frame rate of the game is variable, which it will be. It's very hard to lock frame rate,so, fixed update allows you to update things every physics update.

[00:13:59]
So fixed update is a frame rate independent method, that is called every physics update, typically that's 50 times per second by default you can change that in the project settings. But generally speaking, any kind of physics calculations, that you need to happen, as the game is playing should be done in fixed update, or you're gonna have unpredictable results.

[00:14:23]
So if all of your components that are doing physics operations are running those within fixed update. They'll all stay in sync with one another and create a consistent, physics game world. And again, like I said, that's in contrast to the update method, which is called every frame, and therefore is not frame rate independent it is frame rate dependent.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now