Hi guys and welcome to part 17! In the last part we made our name plates face the camera. Lets leave our galaxy/solar systems for now. In this part we will start looking at how we can build ships in our game.

It’s time to write a couple of new scripts. But first lets tidy up our scripts folder a bit. It’s getting pretty crowded. Create 6 new folders inside the scripts folder. Call them Calculations, Camera, Data Handlers, Empires, Space and UI. Then move all the scripts to the assigned folders in the table below.

Folder Scripts
Calculations PositionMaths
Camera CameraController
Data Handlers TextAssetManager
Empires None (yet)
Space Galaxy, Planet, SolarSystem, SpaceObjects, Star
UI GUIManagementScript

Now that everything is more organised add two new scripts to the Empires folder. Call one Fleet and the other Ship. Make them both pure classes by deleting the “: Monobehaviour” next to the class name and the Update and Start methods.


Start with the Ship class. Let’s think about what ships have in other 4X space games:

  • They have a name
  • They have hull hit points
  • They have shield hit points

Ok so now we add these to our ships. Declare a string called shipName, an int called hullPoints and an int called shieldPoints. Make them all public and add getters and setters to them.

Declaring Our Ship Stats

We also need max hull points and max shield points so add two more ints and give them getters but this time set should be protected.

maxHull and maxShields

Now add a constructor method called Ship and make it public. Pass it shipName, maxHull and maxShields.

Ship Constructor

Set the values equal to each other (set shieldPoints to maxShields and hullPoints to maxHull as a new ship will always start with full hull and shields).

Constructor Complete

So now our Ship class is set up let’s switch over to our Fleet class.

What is a fleet? Essentially all it is is a List of ships. You should know the drill by now… when adding a List, add the line “using System.Collections.Generic” at the top of the script. Then declare a new public list of type Ship called ships. Also declare a string for the fleet name with getter and setter.

Declaring Fleet Stats

Just like our Ship script add a public constructor method called Fleet. Pass it a name and a Ship.

Fleet Constructor

Now we need to set fleetName equal to name and ships equal to a new List of type Ship. We then need to add the passed ship to that List.

Constructor Complete

Let’s also add two new void methods called AddShip and RemoveShip. Make them both public and pass them a Ship.

Setting up the Methods

In AddShip add the Ship to ships. In RemoveShip remove the Ship from ships.

Methods Complete

We have now got a basic data structure for our ships. This seems like a good place to stop, so in the next part we will add a button to add ships to the game and a GameObject to represent our ships.

Scripts_for_part_17 (zip file)

Part 18 Building Ships 2