Welcome to the final lesson in our "Unity and C# Foundations" module! In our previous session, you built a player controller using Unity's modern Input System. In doing so, you attached several new components to a GameObject and even used the [SerializeField] attribute to expose a speed variable to the Inspector. You were already applying today's topic without us even formally defining it.
This lesson will solidify those concepts. We'll formally explore the relationship between GameObjects and Components, which is the architectural backbone of all Unity development. You will learn the different ways to attach components to your GameObjects and, most importantly, how to modify their properties—both manually in the editor for easy tweaking, and programmatically via C# script to create dynamic, responsive game mechanics.
Mastering this concept is what allows different game systems to "talk" to each other, forming the interconnected web of logic that brings a game to life.
The Actor-Component Model
At the heart of Unity is a design pattern known as the Actor-Component Model. It's a simple but incredibly powerful idea.
- GameObjects are the Actors: Think of a GameObject as an empty container or a blank actor on a stage. By itself, it has a position, rotation, and scale (its
Transform), but it doesn't do anything. - Components are the Behaviors: Components are the roles, abilities, and properties you give to that actor. A
SpriteRenderercomponent gives it a visual appearance. ARigidbody2Dcomponent gives it mass and subjects it to physics. A custom script component gives it unique logic.
You've already seen this in action. Your "Player" GameObject is the actor. Its SpriteRenderer, Rigidbody2D, PlayerInput, and PlayerMovement script are the components that, together, make it a movable character.
This approach makes your game design modular and flexible. You can create a "patrolling enemy" by combining a SpriteRenderer, a Collider2D, and a Patrol script. You can then add an Attack script to give it a new behavior without touching the existing ones.
The Ultimate Beginners Guide To Game Development In Unity
This article from freeCodeCamp provides a great analogy for the Actor-Component model, comparing it to building web pages from reusable elements.
Please read the section titled "Unity Builtin Components". Focus on the part that defines the Actor Component model. This will reinforce the idea that GameObjects are the actors and Components are the scripts that give them functionality.
Working with Components in the Editor
The Unity Editor gives you a very direct, visual way to build your GameObjects.
Attaching Components
There are two primary ways to add a component to a selected GameObject:
- The "Add Component" Button: At the bottom of the Inspector window, this button opens a searchable browser of all available components, including built-in ones and your own scripts.
- Drag and Drop: You can drag a C# script file directly from your Project window onto a GameObject in the Hierarchy, or onto the Inspector of a selected GameObject.

This process is straightforward, and you'll be doing it constantly as you build your game.
Modifying Component Properties
Once a component is attached, its properties appear in the Inspector. In the last lesson, you made the speed variable in your PlayerMovement script appear there. This works because you either declare the variable as public or use the [SerializeField] attribute.
public float speed;[SerializeField] private float jumpHeight;
Using [SerializeField] on a private variable is often preferred because it exposes the variable to the Inspector for easy tuning, but prevents other scripts from accidentally changing its value, which helps to avoid bugs.
Properties come in two main flavors:
- Value Properties: These hold simple data like numbers (
float,int), text (string), booleans (bool), colors, etc. You edit these directly in the Inspector. - Reference Properties: These hold a link to another object or component. The
Spritefield on aSpriteRendereris a reference to an image asset. Apublic Rigidbody2D rb;field in your script creates a reference slot for a Rigidbody2D component.

Let's watch a clear explanation of how you create and assign these reference properties. This is a crucial skill for making your components aware of each other.
The Unity Tutorial For Complete Beginners
In this segment from Game Maker's Toolkit's beginner tutorial, the creator demonstrates how to establish a line of communication between a script and a Rigidbody component.
Watch the clip from this section. Pay close attention to how a public Rigidbody2D variable in the script creates an empty slot in the Inspector, and how he then drags the actual Rigidbody2D component into that slot to make the connection.
Modifying Components via Script
While the Inspector is great for setup and tuning, the real power comes from changing component properties at runtime using code. This is how you make things happen in your game: a health script reduces the value on a health bar UI, picking up a power-up changes the player's speed, and so on.
The process involves two steps:
- Get a reference to the component you want to manipulate.
- Access its properties using the reference.
The most common way to get a reference is with the GetComponent<T>() method. This method searches the same GameObject the script is on for a component of type T.
You already did this in the last lesson!rb = GetComponent<Rigidbody2D>();
This line, placed in Awake(), finds the Rigidbody2D component on your Player and stores it in the rb variable for later use. Doing this in Awake() is very efficient because it's done only once when the object is created, rather than searching for the component every single frame.
Let's put this into practice with a concrete example.
C# Scripts as Behaviour Components in Unity! - Beginner Scripting Tutorial
This official Unity tutorial shows a simple but perfect example of a script modifying another component's properties.
Watch the first part of this video, from the beginning to this point. It demonstrates a script that gets a reference to the Renderer component and changes the color property of its material based on keyboard input. This is the exact workflow we're discussing: get a component, change its properties.
Mini-Challenge: Adding a Visual Flip
Let's apply this concept to your PlayerMovement script. We want the player's sprite to flip horizontally to face the direction it's moving. The SpriteRenderer component has a boolean property called flipX that controls this.
- Get the Reference: First, make sure your script can find the
SpriteRenderer. Add a private variable to hold it, and then get the component inAwake(). Your script should now have this at the top:private Rigidbody2D rb; private SpriteRenderer spriteRenderer; // Add this line private void Awake() { rb = GetComponent<Rigidbody2D>(); spriteRenderer = GetComponent<SpriteRenderer>(); // And add this line } - Apply the Logic: Now, in
FixedUpdate(), where you set the velocity, add logic to check the direction of movement and setspriteRenderer.flipXaccordingly.
Can you figure out how to write the if/else if statement to do this? Check the horizontal input (movementInput.x). If it's positive, the player is moving right, so flipX should be false. If it's negative, the player is moving left, so flipX should be true.
Click here to see the solution.
// Inside your FixedUpdate() method, after you set rb.velocity
if (movementInput.x > 0.01f) // Moving right
{
spriteRenderer.flipX = false;
}
else if (movementInput.x < -0.01f) // Moving left
{
spriteRenderer.flipX = true;
}
// We check against a small number like 0.01f instead of 0 to avoid flipping
// when the joystick is barely touched or returns to center.
With this small addition, your character will now visually face the direction of movement, a simple change that dramatically improves the game feel.
Conclusion
Today, you've formalized your understanding of Unity's fundamental architecture. You now have a clear mental model of how GameObjects and Components work together to create complex behaviors from simple, modular parts.
Here are the key takeaways:
- Actor-Component Model: GameObjects are "actors," and Components provide their "behaviors" and properties.
- Inspector Workflow: You can add components and modify their properties directly in the Inspector. This is ideal for setting up objects and tuning values.
- Exposing Variables: Using
publicor[SerializeField]makes your script variables appear in the Inspector as value or reference properties. - Script-based Modification: The
GetComponent<T>()method is your primary tool for finding another component on the same GameObject. - The Control Loop: The common pattern is: get a reference to a component once in
Awake(), store it in a variable, and then use that variable inUpdate()orFixedUpdate()to read or write its properties.
You have now completed the first module and have a solid foundation in the core concepts of Unity and C#. You are ready to move from these abstract building blocks to more tangible parts of your game.
Next Steps
In our next lesson, we will begin Module 2: "2D World and Physics." We will start by implementing the "mechanics-first" workflow you preferred by learning how to import placeholder 2D images and configure their sprite settings in Unity. This will allow us to replace our temporary shapes with actual character and environment art.