Intro
Welcome back to our C# for Unity tutorial series! In our previous post, we covered the basics of C# and how to create and use string variables. Now, it’s time to dive deeper into the world of variables and explore numerical data types. If you haven’t read our previous posts, make sure to check them out first to get up to speed!
Numerical Data Types in Unity
In C#, numerical data types are essential for performing mathematical operations and storing values that involve numbers. Unity supports several numerical data types, but for now, we will focus on two common ones:
- int: Used to store whole numbers, typically ranging from approximately -2 billion to 2 billion. For example, 0, 42, -100, 99999 are all integers.
- float: Used to store fractional numbers with approximately 6 to 7 decimal places. Floats are perfect for storing values that require high precision, such as coordinates or measurements. For example, 3.14f, -1.234f, 100.5f are all float values.
Creating Numerical Variables
Just like we did with string variables, we’ll create numerical variables within our Learning script. Open the Learning.cs script in your project and locate the line that says:
public class Learning : MonoBehaviour
Now, we will create two numerical variables, one of each type. Add the following lines between the curly brackets ({ }) of the Learning class:
// Player’s score
int score = 0;// Player’s health percentage
float healthPercentage = 100.0f;
In the above code, we created an int variable called ‘score’ and initialized it with the value 0. Then, we created a float variable called ‘healthPercentage’ and initialized it with the value 100.0f.
Remember that comments (lines starting with //) are useful for leaving notes about your code, which can be helpful for yourself or others who read your code later.
Using the Variables
Now that we have our numerical variables, let’s put them to use. We’ll update our Start method to display the initial score and health percentage when the game starts. Within the Start method’s curly brackets, add the following code:
void Start()
{
Debug.Log(“Player’s Score: ” + score);
Debug.Log(“Player’s Health: ” + healthPercentage + “%”);
}
The above code uses Debug.Log to print the values of our ‘score’ and ‘healthPercentage’ variables to the Unity console. The + operator is used to concatenate the variable values with the provided strings.
Testing the Code
Before we test our code, let’s make sure we have a GameObject in the scene to attach our Learning script to. Follow these steps:
- Right-click in the Hierarchy panel.
- Select Create Empty to create a new GameObject.
- Rename the GameObject to “PlayerObject.”
- Click on the GameObject to select it.
- Click the Add Component button in the Inspector panel.
- Search for and add the Learning script to the GameObject.
Now, press the Play button at the top of the screen to test the game. Check the Unity console, and you should see the output displaying the initial score and health percentage values.
Conclusion
In this tutorial, we delved into numerical data types in C# for Unity. We learned about the int and float data types and how to create and use numerical variables. By combining string and numerical variables, you can create complex game mechanics and systems.
In the next part of our C# for Unity series, we will explore arithmetic operations and mathematical calculations using these variables. Stay tuned for more exciting lessons!
Remember, practice makes perfect, so experiment with different numerical variables and see what you can create. Happy coding!
0 Comments