In this topic about variables we will be covering the Following variables
BOOL,
STRING AND
INT
its a quite simple version of it lets hope you can understand it!
Bool:
A bool (boolean) variable represents a logical value that can be either true or false. It is commonly used for conditional statements and decision-making in programming.
Example:
// Example 1: Conditional statement
bool isGameRunning = true;
if (isGameRunning)
{
Debug.Log("The game is running.");
}
// Example 2: Function return value
bool hasPlayerWon = CheckIfPlayerHasWon();
if (hasPlayerWon)
{
Debug.Log("Congratulations! You won the game!");
}
// Example 3: Toggle state
bool isLightOn = false;
isLightOn = !isLightOn;
Debug.Log("The light is " + (isLightOn ? "on" : "off"));
String:
A string variable represents a sequence of characters. It is used to store and manipulate text-based data. Strings are enclosed in double quotes (" ").
Example:
// Example 1: String concatenation
string playerName = "John";
string welcomeMessage = "Welcome, " + playerName + "!";
Debug.Log(welcomeMessage);
// Example 2: String length
string text = "Hello, World!";
int length = text.Length;
Debug.Log("The length of the text is: " + length);
// Example 3: String comparison
string password = "password123";
string userInput = GetUserInput();
bool isMatch = (password == userInput);
Debug.Log("Password match: " + isMatch);
Int:
An int variable represents a whole number (integer) without any decimal places. It is used for storing numeric values for calculations and counting.
Example:
// Example 1: Arithmetic operations
int a = 5;
int b = 3;
int sum = a + b;
int product = a * b;
Debug.Log("Sum: " + sum);
Debug.Log("Product: " + product);
// Example 2: Loop iteration
int numberOfItems = 10;
for (int i = 0; i < numberOfItems; i++)
{
Debug.Log("Item: " + i);
}
// Example 3: Conditional statement
int playerScore = 100;
int highScore = 200;
if (playerScore > highScore)
{
Debug.Log("New high score achieved!");
}
Float:
A float variable represents a floating-point number with decimal places. It is used for storing numeric values that require fractional precision.
Example:
// Example 1: Division with floats
float a = 10f;
float b = 3f;
float result = a / b;
Debug.Log("Result: " + result);
// Example 2: Floating-point comparison
float value = 0.1f + 0.1f + 0.1f;
bool isEqual = Math.Abs(value - 0.3f) < 0.0001f;
Debug.Log("Values are equal: " + isEqual);
// Example 3: Conversion to int
float itemPrice = 9.99f;
int roundedPrice = (int)itemPrice;
Debug.Log("Rounded price: " + roundedPrice);