Let’s Make a 4X Space Game with Unity – Part 29 Building Ships with Turns 3

Hi guys and welcome to part 29. In the last part we linked the build cue of the star base with the UI so the number of turns will update when we build a new ship and every time the turn button is pressed. In this part we will finish this off by making the numbers correct for all the ships below the first, have the buttons disappear once the ship is ready and have the ship appear only when the production is complete.

To have our production stack we need to keep track of all the previous ships
production values. In the UpdateShipProductionUI method, in the GUIManagementScript, before the if statement, add a new float called totalProduction and set it equal to 0.

Part029Pic001.JPG
totalProduction

In the for loop we will add our ships production value to this number at the
end of the for loop.

Part029Pic002.JPG
Adding Production

We need to also contain this number in the numberOfTurns equation so it will become:

((ship production value – planet total production) + total production) / planet production

Part029Pic003.JPG
New Equation

Now hit play and check the number of turns is correct for each ship and it changes correctly each time the turn button is pressed.

Part029Pic004.JPG
Correct Number of Turns

Cool, so the next step is to remove the button. We need to remove the button
when starbase production is greater than or equal to the ships production. So in the TurnManager script, in the ApplyProduction method, when we remove the ship from the planets build cue we can also destroy the button. But first we need a bit more code.

Lets make a new method in our GUIManagementScript called RemoveButtonFromCue. Make it public, void and don’t pass it anything.

Part029Pic005.JPG
RemoveButtonFromCue

We want this method to remove the top button from the cue so we need to destroy the GameObject at index 0 in the content object. We can use the transform.GetChild method to get this. Store it in a Transform called topButton.

Part029Pic006.JPG
Getting First Child

Now we just Destroy topButton.gameObject

Part029Pic007.JPG
Destroy the Button

Open up the TurnManager script. We can call RemoveButtonFromCue in the ApplyProduction method now.

Part029Pic008.JPG
Update UI

Hit play and check the button is removed once it reaches the number of
turns required.

This is where there is a bug. If you keep pressing the turn button until the top ship is built, the button disappears but the number of turns is wrong on the rest of the buttons.

Part029Pic010
Correct Turns
Part029Pic011
Incorrect Turns

If you press the turn button one more time the turns return to the correct value but the bug will happen again when the next ship is built.

To fix this we need to go back to our RemoveButtonFromCue method and add a line of code. Before we Destroy the button we need to set its parent to null. This will detach it from the Content object. For some reason just destroying it doesn’t change the child count on the Content object until the turn button is pressed again.

Part029Pic009.JPG
Setting Parent to Null

Finally we need to change our code for building ships so that the ship appears
after the number of turns is completed. This is very straight forward as we did most of the work in part 27. The ships already appear when the turns are up, but we currently have them appearing as soon as the build ship button is pressed (basically having double the amount of ships we want).

To stop them building when we hit the build ship button all we need to do is remove the method from the buttons OnClick in the inspector.

Part029Pic012
BuildShip in OnClick
Part029Pic013
BuildShip not in OnClick

Hit Play and just check everything is working correctly.

That concludes this part. In the next part we will take a break from ships and
start looking at how we can improve our resource system and start getting
resources from planets.

Scripts (Paste Bin)

Part 30 Getting Resources – coming soon

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.