Check If Number Positive Or Negative in Unity with Script

Check If Number Positive Or Negative in Unity with Script

To check if a number is positive or negative, you can use an if statement like this:

if number > 0:
    print("The number is positive.")
elif number == 0:
    print("The number is zero.")
else:
    print("The number is negative.")



You can also use the following code snippet, which returns True if the number is positive and False otherwise:


if number > 0:
    return True
else:
    return False


Here is some more information about checking if a number is positive or negative:

  • In the first code snippet I provided, the if statement checks if the number is greater than 0. If it is, the statement print("The number is positive.") is executed. If the number is not greater than 0, the elif statement checks if the number is equal to 0. If it is, the statement print("The number is zero.") is executed. If the number is not greater than 0 or equal to 0, the else statement is executed, and the statement print("The number is negative.") is printed.

  • The second code snippet is a simple version that returns True if the number is positive and False if it is not. This can be useful if you want to use the result of the check in a program, rather than just printing a message.

  • It's important to note that in both code snippets, the elif and else statements are optional. You can use just an if statement if you only want to check if a number is positive.