Back to Top

Home ยป Tutorials

Unity Game Programming Tutorials

Frequently asked Unity programming questions

This time I have decided to create an article which includes a few questions that I have gotten from you by email - including my answers to them, of course. I am pretty sure that by doing this, many more people will be able to benefit from this information and the included source code.


1) How can I create a fast local Wi-Fi multiplayer game?

First of all, make sure that you've got fast hardware. You will need a fast CPU, plenty of RAM and a decent wireless card. This applies especially to the server; network clients can use normal hardware. By the way, the server doesn't need a fast video card, because it's not supposed to output any graphics.

Moving on to software, you are probably aware that UNet, the former Unity multiplayer solution, has been deprecated. According to its makers, it does not meet modern performance, scale, and security goals. While the old Relay Server and legacy Matchmaker services will continue to work fine for a few more years, I wouldn't start coding a new game which makes use of them.

For now, HLAPI will support both the old LLAPI and the new engine implementation, but the Unity team estimates that the new networking stack will run much faster by 2021. So, since we've already gotten fully functional server hosting services from Multiplay and high-quality communication services from Vivox with the new implementation, I am quite confident that we will be able to create better multiplayer games which run faster than before by making use of the beta version of "Network Transport Layer and Server Runtime".

One a side note, Unity may support Wifi Direct natively in future versions of the engine; in fact, they are running a poll about this on their site right now. So, if you are a fan of Wifi Direct, go to the forum and vote for this feature.


2) Can you show us how to use else-if statements properly?

I have used the "if" conditional statement in my simple tutorials several times; however, else-if statements are perfect if you need to evaluate several conditions at once.

Let's consider this simple program, which tells the player is his driving speed is normal or not.


movement speed

The first "if" branch triggers a "Speed up!" console message if carSpeed is less than 20 mph, the second one outputs "Normal driving speed" at the console if carSpeed is at least 20 mph and (&&) less than 40 mph, while the final "if" condition is triggered if the speed of our car is at least 40 mph.

Can you imagine the complexity of our code if we had to evaluate 10 speed intervals? Well, that's exactly where those else-if statements can be of great help. Here's a rewritten version of the code.


else-if example

As you can see, the new version of the code is much cleaner and does exactly the same job. Set up various carSpeed values in the Inspector, click the Console tab to view the output, and you will see what I mean.


3) How can I choose a random array value? I want to pick a random name from a list.

Add a RandomName C# script to your Unity project, and then put the code below inside it.


random names

Don't forget to create an empty object in the Hierarchy view, and then drag the RandomName script over it, or add it as a component. If everything works as expected, you will see a "Player names" field in the Inspector view. Expand it using the tiny triangle on its left, and then choose the size of the string array and populate it with the desired names. I chose to use five names for my example.


string array

Run the project; since we are using the "Start()" method this time, you will see a fresh name being picked up and displayed at the console anytime you run start the application.


console output