Let’s Make a 4X Space Game with Unity – Part 25 Star Bases 4

Hi guys and welcome to part 25. In the last part we worked on our UI so that when we click on a planet with a star base, we get a star base panel with a build ship button. In this part we will set up a build cue.

Create a new child on the starbase panel; a scrollbar object (under the UI option). Rename this Build Cue List.

Part025Pic001.JPG
Adding Build Cue List

Align the object something like the picture below and disable the horizontal option in the Scroll Rect component in the inspector.

Part025Pic002.JPG
Scroll Box Aligned
Part025Pic004.JPG
Scroll Rect Settings

Find the Content object (Build Cue List > ViewPort > Content) and add a vertical layout group to it. Select both Control Size and Child Force Expand width options but make sure both height options are deselected. Give a padding of 3 all the way around and leave spacing at 0.

Part025Pic003
Content
Part025Pic006.JPG
Vertical Layout Group Settings

Create a button and make it the child of the Content object. Rename the button in the hierarchy to Ship Blueprint Button.

Part025Pic005.JPG
Ship Blueprint Button

Align the text left and then move the object right slightly so it isn’t touching the edge of the button. Change the default text to “Ship”.

Part025Pic007.JPG
Text Settings
Part025Pic008.JPG
Move Object Slightly to the Right

Add another text object to the button and align this one right. Change the default text to 1. Change the name of the two text objects; this new one to Turns Text and the other to Ship Title Text.

Part025Pic011
Text Settings
Part025Pic009
Text on Button
Part025Pic010
Renaming the Text Objects

Drag the Ship Blueprint Button into the prefab folder to make it a prefab. Then delete the one in the hierarchy.

Part025Pic012
Prefab

We need a way of adding a button to the list when we hit the build ship button.

To do this lets create a new script and add it to our Content object. Call this script Instantiation. In the code add a public GameObject called objectToInstantiate and delete the Start and Update methods.

Part025Pic013
Instantiation

Next add a list of GameObjects called objectsInstantiated and set it equal to a new list of type GameObject.

Part025Pic014
objectInstantiated

Add a public void method called InstantiateObject, do not pass it anything.

Part025Pic015
Method

In InstantiateObject simply add code to instantiate objectToInstantiate and set the parent to this. Also add the object to objectsInstantiated

Part025Pic016
InstantiateObject

We need a way to remove the button from the list so add another public void method called RemoveObject. Pass this one a GameObject and add an if statement which checks the passed GameObject is in the list we created. If it is we need to remove it.

Part025Pic020.JPG
RemoveObject

 

Now we have a simple script we can apply to other objects later on if we need to (planet building que UI, job list UI etc…). To complete our ship building UI however we need to link our build ship button with this new script.

First add our Ship Blueprint Button to the GameObject slot in the Instantiation script on Content.

Part025Pic018.JPG
Content

On the Build Ship Button add a new OnClick. Set the Object as Content and then the function as InstantiateObject.

Part025Pic019.JPG
New OnClick

Hit play and check the Ship Blueprint Button appears when you press the Build Ship Button.

Next we need to destroy the buttons when they are clicked. Add a Ship Blueprint Button into the Hierarchy so we can edit it. To destroy the buttons it is a little bit more complex, we first need to add a script to the button called ClearBlueprintButton.

Delete the Update method and add a Instantiation called ins (just to keep it simple). In the start method set ins equal to the Instantiation in this objects parent.

Part025Pic021
Setup

Lastly add a method called DestroyObject. In this object call the RemoveObject method in ins passing it this GameObject. Then Destroy this GameObject.

Part025Pic022.JPG
DestroyObject

Now all we need to do is add the DestroyObject method to the Ship Blueprint Button OnClickDon’t forget to apply the changes to the prefab and then remove it from the hierarchy.

Part025Pic023.JPG
OnClick

Hit play and check that you can add buttons to the list and delete them by clicking on them.

That concludes this part.

Before I end the blog I just want to address whether going forward this game will be turn based or real time. I would love to show you guys how to make both, however I do not have enough time to do that. From what I have read online and some conversations I have had with people following this tutorial I have decided that this will be a turn based game.

Therefore in the next part we will start to make a turn based system so it takes a number of turns for our ships to build.

Part 26 Turn System

Advertisements

4 Comments »

    • Hi Kohi, sorry its been a while life as been very busy the last couple of months. I am aiming to have the next part up by Feb 14th.

Leave a Reply

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