RefactoringCode&Prefab — UnityGameDebut

Let’s Refactoring The Code — Episode #06

J3
KidsTronics
4 min readMar 4, 2018

--

Hi, you there! Here is the previous code. Our solitary enemy is moving to the left and back … let’s add another enemy to the scene. Let’s go straight to the code refactoring:

Step #01 — Let’s pick Soldier.cs up to where we left off:

Refactoring Soldier.cs code by transport the last 3 lines to a new method and name it says MoveLeftwards() — use the lamp bulb icon; type:

Step #02 —Copy & paste and make another method in Soldier.cs named MoveRightwards(); invert the angle; save everything;

The moment the enemy is in the left position he will shoot at us:/ when he’s in the right position he must go back to the idle status…but this is for further down the road (TODO list);

Step #03 —In Soldier.cs, Use Invoke() on Start() to call the MoveRightwards() after 3s; see:

Step #04 — Now we need 3 variables in Soldier.cs to control the time the enemy is in the left (LeftTime), shooting at us (ShootTime) and in the right (right time) positions:

Substitute all the magic numbers in Soldier.cs

Step #05 — Let’s add Soldier.Activate() so that the method that controls the enemy status is not in the Start(), and let’s make it callable (public) from other objects :

The enemy moves to the left position and returns back to the right and remains there. Meanwhile, he must shoot at us, right? For this, we need a controller: EnemyController.cs responsible for the soldier's behavior control. Here’s the concept:

Note that each soldier has it’s own behavior, but EnemyController controls them all together.

Step #06–Create an 1) Empty gameObject, 2) rename it to EnemyController, 3) right mouse click, Create > C# Script, name it EnemyController.cs, and 4) drag n’ drop this to gameObject just created;

Step #07 —Using Prefabs: 1) Create a folder Assets >Prefabs, then create a prefab by selecting 2) Assets > Create Prefab inside this folder, then 3) dragging Soldier gameObject onto the empty prefab asset that appears, and 4) finally rename it to Soldier; Simply dragging the prefab asset from the project view to the scene view will then create instances of the prefab. Objects created as prefab instances will be shown in the hierarchy view in blue text. (Normal objects are shown in black text);

Step #08 — Now 1) add prefab Soldier to the scene and positioning it at the third tower; 2) Rewrite its name, Soldier(1) and Soldier(2);

Step #09— Go to Assets > Scripts > EnemyController.cs and add an array of Soldier named Enemies (save it) :

Step #10 — Return to Unity and 1) Select EnemyController on Explorer, then you’ll see these property Enemies — since it’s public; 2) type size=2 and them 3) Drag n’ drop the objects Soldier(1) and Soldier(2) to the slots created;

That’s enough for today’s post! We’ve prepared a script for controlling all the enemies at once. Here are the codes till here:

The Crosshair.cs does not change…see Part_4.

Next post we will make the soldiers appear randomly.

Thanks for your visit! Stay tuned!

# 0 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10

GitHub Repo

References & Credits

Making a Game with Unity (1–10): Basics by Jayanam

Prefabs

--

--

J3
KidsTronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️