Hi guys and welcome to part 16. In the last part we named all our stars using a txt file. This txt contained 300 names which meant we couldn’t have more than 300 stars in our galaxy. In this part we will fix this. It will mean 300 of our stars will have names and the rest will be boringly called Star X where X is a number but at least we can add more. We will also make our name plates follow the angle of the camera.

1. Adding More Stars

It is a really easy fix to allow us to add more stars. Open the Galaxy script and go to the CreateStarData. Declare a new string called name and randomIndex at the top of the method not assigning them any value.

Declaring name and randomIndex

Then add an if statement with the condition if availableStarData has a count greater than 0. Add an else statement afterwards too.

If/Else Statement

Move setting randomIndex to the if statement and remove the int part. Set name to the string at availableStarNames’ index randomIndex. Finally, move the line where we remove the string from the list to here.

If Statement Complete

In the else statement make name equal to “Star ” + starCount. Also, where we create starData sub in name to where we pass the starName.

CreateStarData Complete

Now we can add more stars to our game without an error occurring :).

2. Rotating the Name Plates

So how do we get the names to face the camera? We use the Transform.LookAt function. However, it is a little bit more complex than that because as you will see the LookAt function doesn’t quite work the way we want it to. We also need to consider the fact we need to update all 300 name plates constantly to face the camera. 300 Update methods uses a lot of resources so let us consider only having one update method monitoring where the camera goes instead. For this we will be adding a new object to our scene.

In Unity add a new empty object called GUI Manager. Attach a new script to it called GUIManagementScript and add an OnEnable method to the script. Leave the Start and Update methods for now.

GUI Manager
Setting Up GUIManagementScript

We will only have one of these objects in our game so we will create an instance of it much like we did with our Galaxy Manager and Solar System Manager objects. Declare a new public static GUIManagementScript called GUIManagerInstance. In the OnEnable method set it equal to this.

Setting GUIManagerInstance

Add “using System.Collections.Generic” to the top of the script and declare a new public List of type GameObject. Call this list namePlates. Set it to a new List in the OnEnable method.


Now in the Update method add an if statement with the condition if namePlates has a Count greater than 0. Inside that add a for loop with the limit of namePlates‘ Count.

Setting Up the Update Method

Note: We want to cycle through all the GameObjects in namePlates and make them look at the camera. This is going to be a very expensive script (i.e. it will take up a lot of time/CPU relatively speaking). However, because we don’t have many things going on in our game yet, it’ll work… for now. In later tutorials when we come to improve the efficiency we will look at ways of making this script a lot more efficient. Like everything in this tutorial series I just want to get things to work before improving them later on.

Use i as the index in the namePlates list and set the rotation of the object to look at the camera (see the picture below for help).

Looking At the Camera

Press play. That’s not right, all the names are backwards! This is not what we want. We need to rotate the name plates by 180 degrees around the Y axis.

Name Plates are Backwards

But how do we do that? After we make the GameObject look at the camera declare a new float called rotY and set it equal to the GameObject’s localEulerAngles.y plus 180.


Now all we do is set the localEulerAngles of the GameObject to equal a new Vector3. For the X componant we use the negative current localEulerAngles.X. We also do this for Z (except we use localEulerAngles.z). For Y we use our rotY we just calculated. 

Correcting the Rotation

Note: Notice how I have separated the values in the Vector3 on separate lines. This is a perfectly acceptable way of doing things and is good practice if the line of code you are writing is wider than the viewable area in your coding program. It makes it easier for others and yourself to read your code.

Press play. Pan the camera around and zoom in and out. All the text should now be facing the camera :).

Name Plates Looking at the Camera

There’s just one more thing we need to do. At the moment namePlates will just keep getting bigger and bigger every time we exit and enter the galaxy and solar system views. For this reason we need to clear the list when that happens. To do this we just add a single line of code to our DestroyGalaxy and DestroySolarSystem methods (in Galaxy and Solar System respectively). This line of code uses the Clear function for a List. See the pictures below.


Now namePlates will never grow to be bigger than the number of stars in our galaxy :). That concludes this part. In the next part we will take a break from the galaxy and solar system set up and start to look at how we add ships to our game :).

Scripts_for_part_16 (zip file)

Part 17 Building Ships