Hi guys and welcome to part 5 of this series. In the last part we introduced a seed number to our galaxy and got some feedback in the console when we click on the stars. In this part we will finally make it so we can view the solar systems!

Lets jump right in. Open up the Galaxy script. We want to make all our spheres be children to the Galaxy Manager object so we will be using Transform.SetParent method. In the CreateSphereObject method after we set the position of the sphere set the parent to the galaxy manager.

part005pic001
Setting the Parent as Galaxy Manager

Now if we press play all the spheres will be neatly organised under the Galaxy Manager object in the hierarchy.

We are essentially going to use the same method in our Galaxy script so copy and paste the CreateSphereObject method into the SolarSystem script. Change the passed Star to a Planet and change starGO to planetGO.

NOTE: This is a bad way to code!  We should really make a new class and method we can call from both the Galaxy and SolarSystem scripts. However as this is a tutorial I am making the code as explicit as possible and getting things to work. Before the next part I will tidy this up and make it better. The updated code will be provided and I will post a change log so everyone can follow what I have changed.

Part005Pic002.JPG
Creating Planet Objects

When we click on the star we want to make a solar system appear and the galaxy disappear. This means we need a method to destroy the galaxy. Yep that’s right, we are about to destroy the galaxy.

Open the Galaxy script and add a new method called DestroyGalaxy. Make it public and void with nothing passed to it. Add a while loop. The condition of the while loop will be while the Galaxy Manager has a child.

part005pic004
DestroyGalaxy Method Setup

In the loop we take the first child, make it parentless (i. e. its own GameObject in the hierarchy) and then destroy it.

Part005Pic003.JPG
DestroyGalaxy Method Complete

We can now call this method in the SolarSystem script when we click on a star.

Part005Pic005.JPG
Calling DestroyGalaxy

Press play and check the galaxy disappears when you click on a star.

Let’s make a new method in the SolarSystem script called CreateSolarSystem. Make it public and void with a Star passed to it.

part005pic006
CreateSolarSystem Method Setup

When we view the solar system we want the star to be at the centre of the scene so our first line should be to create a basic sphere at (0,0,0) with the same name as the star. Don’t forget to make the object a child of the Solar System Manager (this will be important later).

Part005Pic008.JPG
Creating the Star in our Solar System

Note: This code is very similar to our createSphereObject method. Again this is bad coding and will be cleaned up by the next tutorial.

Now we need to create the planet objects. We will use a for loop and the CreateSphereObject method to do this. However before we call the CreateSphereObject method we need to create a Vector3 for our planet placement.

In our Galaxy script we have a method which creates a Vector3 called RandomPosition. This is not suitable however, as this uses the max and min radius of the galaxy to create a distance. Our solar system will be smaller than the galaxy and the distance should really be based on the order of the planets from the central star. For now, we will have the planets an equal distance away from each other based on the planet order.

Add a for loop to the CreateSolarSystem method with the limit of the stars planetList count. Declare a Planet called planet equal to the current planet in the loop. Then declare a float called distance and make it equal to the current number in the loop plus one then multiplied by five. Make a second float called angle and make it equal to a random number between 0 and 2 * Pi.

Part005Pic009.JPG
Starting the For Loop

Now we can use the same maths in RandomPosition in the Galaxy script to get a Vector3 (call it planetPos). Then it is just a case of calling CreateSphereObject and passing it planet and planetPos.

part005pic010
Calling CreateSphereObject

The last thing to add is the call to CreateSolarSystem after calling DestroyGalaxy.

part005pic011
Calling CreateSolarSystem

If you press play now and click on a star a solar system should appear!

That concludes this part. You might notice we have no way of returning to the Galaxy view yet. That will be what we cover in the next part :).

scripts_for_part_5 (zip file)

Updated Code (read before part 6)

Part 6 Resetting the Galaxy