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.
In the for loop we will add our ships production value to this number at the
end of the for loop.
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
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.
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.
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.
Now we just Destroy topButton.gameObject
Open up the TurnManager script. We can call RemoveButtonFromCue in the ApplyProduction method now.
Hit play and check the button is removed once it reaches the number of
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.
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.
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.
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