Hi Guys and welcome to Part 20. In the last part we disabled the build ship button in the galaxy view, linked our ships to their GameObject and destroyed the ships when we return to the galaxy view. In this part we take a bit of a break from our ships to address a bug which was found by Infinitea. It seems that on some systems (doesn’t seem to be an issue on my surface pro which I do the majority of the coding on, but is an issue on my PC) when exiting certain solar systems the galaxy alternates between two states. It seems this is due to the solar system not being destroyed before the galaxy respawns and so a collision occurs changing the location of the stars.

So how do we fix this? Well I was planning on doing this a bit further down the line but there is a way we can both improve the efficiency of respawning the galaxy and fix the bug at the same time so I have brought it forward to now.

Open up the Star script. We need to declare a new public Vector3 with get and set. Call this starPosition. Inside our Star constructor set this equal to a new Vector3

Setting starPosition to New Vector3

Now open up the Galaxy script. In the CreateArms method, in the if statement that checks if collision is not true, set starPosition to cartPosition.

Setting starPosition to Calculated Position

Do the same for the CreateCentre method.

Create a new public void method called ResetGalaxy.


In this method declare a list of type Star and call it starList. Set it equal to starToObjectMap.Keys.ToList<Star>. Then we need to clear starToObjectMap so set it equal to a new Dictionary<Star, GameObject>. We need to clear it otherwise the dictionary will add more and more Stars and GameObjects every time we reenter the galaxy view.

List and Dictionary Reset


Now add a for loop with the limit of starList.Count. In this for loop declare a new Star called star and set it equal to the current index i in starList.

Star at Index

Next we create the GameObject for the Star using CreateSphereObject (pass the new Vector3 starPosition to this method). Call this starGO.


Finally we add star and starGO to our dictionary and set galaxyView to true after the for loop.

Adding to Dictionary

To make this work all we need to do is change the function which is called on the Galaxy View Button from CreateSpiralGalaxy to ResetGalaxy.

Changing the Button Click

Press play and check everything works. 

This is much more efficient :). Previously the game recalculated the Galaxy positions every time we hit the galaxy view button. Now it calculated them once at the start of the game. It also fixes that pesky bug.

We could also do this for our planets too. This will make our solar systems more efficient too. Have a go yourself :). I will post how to do it in a week or two but I don’t think I will make it an official part of the tutorial.

In the next part we will go back to our ships. There are a lot of things left to do with them (movement, targeting, info displays etc…) however I think the first thing we will tackle is the fact they are currently free to build, so next time I will introduce resources.

Scripts_for_part_20 (zip file)

Part 21 Resources