C# Programming – Ant’s Unity Challenges (Random Name Generator)

Unbeknownst to a lot of people, there is a handy little PowerPoint presentation on the shared drive titled ‘Unity Challenges’; it is currently a work in progress as Ant is still updating it and isn’t finished with it as of this writing but it is a neat, little extension task of sorts for those looking to get into programming.

At the end of one my previous blog posts I briefly mentioned how I had completed the cookie clicker challenge from that presentation. That challenge was a rather simple one to complete as it was simply a matter of adding one to the score count every time the “cookie” was clicked on. However, I have now started attempting more advanced challenges now that my skills have developed. Back when I completed the first challenge, it was the only one I felt capable of doing but now my advancements show thought he fact that I was able to complete these additional tasks.

Unity Challenge - Random Name Generator

This challenge is one that seems really daunting upon first glance. After all, I haven’t even touched upon randomness within C# aside from briefly in the OneTwoThreeGame (in this case, it was used to add either 1,2 or 3 to the target value).

As is the case with the entirety of programming, things start coming together once you take your time and tackle the task at hand systematically and logically. The challenge clearly stipulates that it wants a random forename and a random surname to be paired up with one another. In addition to this, there was also to be a 20% chance that a random middle name would also be added to the mix. 

NameGenerator

The first step was setting up the variables as you can see in the above class diagram. Each name had 3 variables linked to it: a string array, an int to determine which number in that array is chosen and a string to display the result of the randomised number.

Since this was being controlled by a button, I didn’t need start or update functions hence why I went with one public void called nameGeneration. 

At the forefront of this function, I used a conditional to ask if the Random.Value is > than 0.8. Confusingly, what this actually means is that there is a twenty percent chance of the contained block of code executing. I had to read up on this one and it was hard to grasp but I somewhat understand this now. 

Now, since this particular block of code was the block executed on the off chance that a middle name is selected, I had to ensure to randomize all 3 names. For each one, I set the outcome int variable equal to the Random.Range of 0 and the length of the string array (I remembered that the final value in the bracket is an exclusive and is therefore excluded from the range which is fine as Unity begins on element 0 anyway when it comes to arrays). Then, for each name yet again, I set the result for the string equal to the string array with the number in the square brackets ([]) being equal to the outcome int. Believe it or not, this was the randomizing and hardest part over and done with. 

All that was left to do was display this with a debug.log and also display it in the form of UI text thus using ToString(). 

After this was completed, I needed to write up a block of code that executes for the other 80% where the middle name is not given. This was rather simple. All I did was add and else to the end of our previous if conditional – copying and pasting the forename and surname lines of code and removing the middle name from the debug.log and UI text.

This was the scripting done. The next step was simply dragging this into the OnClick and writing up some forenames, surnames and middle names. 

Overall, I was really happy with the fact that I was able to complete this effectively. It has definitely helped me improve as I now know how to specify percent chances within C# as well as being more familiar with randomness in general.

Until next time,

Luke

 

 

Leave a comment