Hi guys and welcome to part 18. In the last part we created a data structure for our ships and fleets. In this part we will use these structures to create game objects to represent our ships.

In Unity add a new Button as a child to our Canvas object. Name this button Build Ship Button. Move it and anchor it to the top left corner of the screen (click on the square picture under it’s Rect Transform component).

Build Ship Button in hierarchy
Settings for Button

Change the text it displays to “Build Ship” by selecting its child object called Text and finding the text box in the inspector.

Text Object
Changing the Text
Game View

Ok so we have a button but it doesn’t do anything yet. Let’s create a new Object in the scene called Player and attach a new script called FleetManager to it. Put FleetManager in the Empires folder (in the Scripts folder).

Player Object

Open up the new script and add “using System.Collections.Generic” and “using UnityEngine.UI at the top.

Adding Libraries

Declare a public list called fleetList of type Fleet. Delete the Update method as it won’t be required and in the Start method make fleetList equal to a new List of type Fleet.

Constructing fleetList

Add a new public void method called BuildShip. It doesn’t need to be passed anything for now.

Setting Up BuildShip

So what is going to happen when we build a ship?

  1. A new Ship class will be created.
  2. The Ship is added to a new Fleet class.
  3. The Fleet class will be added to the fleetList.
  4. The GameObject will be created.

With this in mind declare a new Ship called ship and a new Fleet called fleet.

ship and fleet

Notice how there are red lines under the Ship and Fleet. This is because they are not valid. They both need things passed to them.

What do we do for ship? For now we will just call every ship “Ship X” where X is a number. But how do we get this number? Let’s declare a new int at the top of the class called shipCount. It can remain private for now. Then pass the new Ship “Ship ” + shipCount for its name.

Note: Later on we will use name lists for the ships like we did with the star systems 🙂

Naming ship

All that is left for ship is to add the max hull and max shields. For now I am just going to put 10 for each but feel free to put what ever numbers you’d like.

ship Complete

For fleet we should name it “Fleet ” + (fleetList.Count + 1). The plus one is because we haven’t add the fleet to the list yet. Then we also pass it ship.

fleet Complete

Then we add fleet to fleetList.

Adding fleet to fleetList

Now all that’s left is to create a GameObject to represent our ship. Let’s be like the Borg and use cubes for our ships (for now). Let’s also position the ships in a random location in the radius of our solar systems (in my game this is 50).

Note: If you have not got the PositionMath script yet you can find it here (paste bin)

Creating the GameObjects

In Unity select the Build Ship Button and in the inspector scroll down to the OnClick() section. Click on the + and as the object select Player. As the function select the BuildShip method under the FleetManager selection.

Setting Up the Button

Hit play and press the build ship button. You will see cubes appearing in random locations (it looks better in the solar system view).

I think this will be a good place to stop. In the next part we make it so we cant build ships in the galaxy view and link our ship data with the objects.

On a side note: Sorry there was a long delay in getting this part of the tutorial online, I recently started a new job and things have been a bit crazy. Add this site on twitter (@SP_Coding), I am going to start using this as an update tool to let you guys know what’s going on and when you can expect new posts. All posts are linked to this account as well so if a new one goes up you won’t miss it :).

Scripts_for_part_18 (zip file)

FleetManager (paste bin)

Part 19 Building Ships 3