0
0
Unityframework~30 mins

Component communication in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
Component communication
📖 Scenario: You are creating a simple Unity game where two components need to communicate. One component will keep track of the player's score, and another component will display the score on the screen.
🎯 Goal: Build two Unity components: ScoreManager to store and update the score, and ScoreDisplay to show the score in the console. Learn how to make these components communicate by accessing each other's data.
📋 What You'll Learn
Create a ScoreManager component with a public integer variable score initialized to 0
Create a ScoreDisplay component that accesses ScoreManager to read the current score
Update the score in ScoreManager by adding 10 points
Print the updated score from ScoreDisplay using Debug.Log
💡 Why This Matters
🌍 Real World
In many games, different parts of the game need to share information, like player health, score, or inventory. Learning component communication helps you build interactive and connected game systems.
💼 Career
Game developers often write components that must talk to each other. Understanding how to share data between components is a key skill for creating maintainable and scalable game code.
Progress0 / 4 steps
1
Create the ScoreManager component
Create a C# script called ScoreManager with a public integer variable score initialized to 0.
Unity
Need a hint?

Remember to make the score variable public so other components can access it.

2
Create the ScoreDisplay component and reference ScoreManager
Create a C# script called ScoreDisplay. Inside it, create a public variable scoreManager of type ScoreManager to hold a reference to the ScoreManager component.
Unity
Need a hint?

Declare a public variable of type ScoreManager so you can assign it in the Unity Editor.

3
Update the score in ScoreManager
In the ScoreManager class, add a public method AddPoints that takes an integer parameter points and adds it to score. Then call AddPoints(10) inside Start() to add 10 points when the game starts.
Unity
Need a hint?

Use a method to change the score and call it in Start() to simulate adding points at the beginning.

4
Display the updated score in ScoreDisplay
In the ScoreDisplay class, add a Start() method that uses Debug.Log to print the current score from scoreManager.score.
Unity
Need a hint?

Use Debug.Log inside Start() to print the score from the scoreManager reference.