hi i was trying to make my character stop walking when the punching animations starts but i get this error :
(68,44): error CS1525: Invalid expression term 'bool'
here is the script
1 messages · Page 1 of 1 (latest)
hi i was trying to make my character stop walking when the punching animations starts but i get this error :
(68,44): error CS1525: Invalid expression term 'bool'
here is the script
Look at the line with the error
i did it's this one :
(if (direction.magnitude >= 0.1f && bool("StopWalking" == false))
but i don't understeand how to solve the error
The error is telling you that bool is an invalid expression here. Why on earth did you write bool("StopWalking == false), as if it were a function?
i writed it cuz i was trying to tell the code that if the bool was false than the character could walk, so that is the only thing that i could think of
but why did you write bool(...)
that's not how if statements work
It's just if (a && b), not if (a && bool(b)), that makes no sense
ok so i just need to write :
if (direction.magnitude >= 0.1f && "StopWalking" == false)
right?
no, because you can't compare a string to true/false
and even if you could, it would be a pointless check. "StopWalking" cannot be equal to false. because it is equal to "StopWalking"
then how can i make the player stop if i don't use a bool to tell the code when to stop?
cuz u just said that i can't use a bool in that code
I didn't say "a bool", I said "bool", the literal usage of the keyword
oh ok
this line is nonsense. bool is not a function
you can't call it like a function
ok so i just need to check if StopWalking is false without using "bool" cuz it's not a function right?
well no because of the other thing I said
^
oh
you're comparing completely unrelated things. it's as if you are picking up an orange and saying "is this equal to my name?", when such a question is meaningless. your name is not a fruit, and a fruit is not your name
similarly, a string is a variable that contains text
false is a boolean value
text != boolean
they are not comparable
ok now i understeand
create a bool variable, set it to true/false when you need to, and check that
but how can i tell the code to check if it's true or false i can't just make an if statement inside an if statement?
actually a better analogy than an orange would have been a light switch. since that has two states - on or off. similar to bool, true or false. but you get the picture
you can make an if statement inside an if statement
oh i didn't knew
but you don't need to, because you are using && already
if (conditionA)
{
if (conditionB)
{
// code
}
}
is exactly equal to
if (conditionA && conditionB)
{
// code
}
so i can just write this :
if (direction.magnitude >= 0.1f && "StopWalking")
no that's still a string
^
create a bool variable. bool stopWalking; or something
and then check if (direction.magnitude > 0.1f && !stopWalking)
oh i just need to put a ! then
! means "not", so it will check the opposite is true.
if stopWalking is false, you presumably don't want to stop walking, and so !stopWalking gives you the opposite - true. this is true, the magnitude > 0.1 is true, the condition will execute
if it is true, the opposite is false. the fact it's false means it will not execute
ok ty for the help
so i think i made another mistake, trying to set the bool to false and true i wrote this :
if(Input.GetButtonDown("Fire1") && isGrounded)
{
anim.SetBool("Punch", true);
bool StopWalking = true;
}
else
{
anim.SetBool("Punch", false);
bool StopWalking = false;
}
but unity gives me this warning :
warning CS0219: The variable 'StopWalking' is assigned but its value is never used
Dont write bool before each StopWalking since you already declared it at the top of your code (I assume)
Just declare StopWalking once and use it
Then how can i change StopWalking to true and false?
Ok
Just declare once in your code
bool StopWalking = false;
And whenever you want to change value do
StopWalking = true