Hey guys and welcome to part 6. In the last part we were finally able to view solar systems! But now we have no way to get back to the galaxy view! That is what we will work on today :).

First things first, I made some changes to the code from last time to tidy things up a bit. If you haven’t already seen the change log or downloaded it you can get it from here:

Updated Code

Let’s start with a quick example on how we use the new scripts. Open up the SolarSystem script. In the CreateSolarSystem method, where we create our star object, comment out the code.

Commenting Out the Code

Tip: Always comment out code before changing it. That way if the changes don’t work you don’t need to type it all out again.

Now underneath the commented out code we will use the CreateSphereObject method in our SpaceObjects script. Add “SpaceObjects.CreateSphereObject(star.starName, Vector3.zero, this.transform).

Using a new Script

Note: If you look in the SpaceObjects script and look at the CreateSphereObject method, you will notice where the transform is passed it equals null. For those of you not familiar with this, this is a way of saying the transform is optional. If no transform is passed it will automatically set the transform as null. You can use this for any type of variable for example you might have a method that is passed an int but the int is a certain number 9/10 times. You can make it equal to that number in the method pass and then you only need to actually pass an int the 1/10 time that number is different.

If you are in anyway confused feel free to comment at the bottom and I will try to help. Now that’s taken care of let’s get back to progressing our game.

So far when we click on a star the view changes and we see a solar system but we cannot get back to the galaxy view unless we stop the game and restart it. A good way to get back to the galaxy view would be to add a button to the scene the user can press to get back to the galaxy.

In the hierarchy right click to bring up the menu. Scroll down to UI and add a button (a new canvas will appear automatically as a parent to the button). Rename it Galaxy View Button. At the top of the scene view click the 2D button to make it easier to see and move the Galaxy View Button to the bottom right corner.

Galaxy View Button in Hierarchy
Galaxy View Button in the Scene View

Back in the hierarchy, select the Galaxy View Button’s child (its called Text). In the inspector change the text from “Button” to “Galaxy View”.

Text Component
Changing the Text

Don’t forget to save the scene.

At the moment this button does nothing. Let’s change that. Open up the Galaxy script and add a new public void method called CreateGalaxy. It doesn’t need to be passed anything. Go to the Start method of our script and from where we create a new dictionary for StarToObjectMap until the end of the Start method cut/paste all this code into our new Method.

How the Start Method Should Look After Cut
How the CreateGalaxy Method Should Look After Paste

Don’t forget to call CreateGalaxy in the Start method after SanityChecks!

Start Method Completed

Now we have a method we can call from the button. In the OnClick section of the buttons inspector click the + and add the Galaxy Manager Object. In the function drop down menu select Galaxy and then the CreateGalaxy method.

OnClick Setup

At the moment the button can be pressed at any time and that can cause multiple copies of the galaxy to spawn. Also, after we have clicked on a star, the solar system hangs around after we return to the galaxy view. We can fix this by adding code to the SolarSystem script.

At the top add “using UnityEngine.UI” (this will give us access to the UI functions in Unity). Then after we declare SolarSystemInstance declare a public button and call it galaxyViewButton.

Adding galaxyViewButton

In the OnEnable method add a line using the Selectable.interactable method to make the galaxy button not intractable (unselectable) at the start of the game (while we are on the galaxy view).

Making the galaxyViewButton Unselectable

Now in CreateSolarSystem use the same method again but this time make it selectable.

Making the galaxyViewButton Selectable

Add the Galaxy View Button to the galaxyViewButton spot on the Solar System Manager inspector.

Setting Up the Button

Now we just need to solve the problem of the solar system hanging around.

In the SolarSystem script add a new method called DestroySolarSystem. Make it public and void with nothing passed to it.

Setting Up DestroySolarSystem

This is going to be very similar to the DestroyGalaxy method. Add a while loop with the condition while the Solar System Manager has a child. Declare a game object equal to the first child. Make the object parentless and destroy it.

DestroySolarSystem Destroying Stuff

Note: This is the same as the DestroyGalaxy code. Usually I would say this is bad code and we should just make a single method to handle destroying stuff. However I want to keep these separate for now as we will be adding some stuff later which makes these slightly different. I feel the tutorial will be clearer if they are two distinct methods.

After the while loop copy and paste the line we used to make the button unselectable in the OnEnable method to make the button unselectable once more.

DestroySolarSystem Complete

Click on the Galaxy View Button in the hierarchy. In the OnClick section click the + button again to add another function.

Really we want the solar system to be destroyed first so copy the first object and function into the second slot (i.e. Galaxy Manager object and CreateGalaxy function). Then change the top object to Solar System Manager and the function to DestroySolarSystem (under the SolarSystem tab in the drop down menu).

Button OnClick Complete

While we have the button selected there’s one last thing I forgot to mention before. At the top of the button inspector under the Rect Transform heading we need to anchor the button to the bottom right corner. Click on the square picture. While holding alt and shift on the keyboard select the bottom right corner.

Anchored to Bottom Right Corner

Save the scene and press play. Now we can click on the star, view the solar system and then return to the galaxy view by hitting the button!

That concludes this part. In the next part we will start working on a camera control system so it is easier to view our galaxy and solar systems!

scripts_for_part_6 (zip file)

Part 7 Camera Control