Greater than or less than if statments
I don't understand why this doesn't work
var timer1; var holdtime; if (Input.GetKeyUp(KeyCode.W) && timer1 > holdtime) {}
Unity keeps telling me that I cant use this symbol (>) between the object and the float. Also I wouldn't mind making it a greater than or equal to rather than just greater than if that is possible. I know this is a noobie question but I am new and cant find any answers.
Answer by UnityCoach · Apr 29, 2017 at 05:34 PM
I'm surprised you can use var
without assigning it any value. Like @FortisVenaliter said, type your variables, it'll make it easier. You can use >=
or <=
for greater/less than or equal to.
float timer1;
float holdtime;
if (Input.GetKeyUp(KeyCode.W) && timer1 >= holdtime)
Thank you! I think the problem is fixed now... time to fix the other 1000 problems.
I know what you mean :) If you're interested and have time, check out my website and subscribe to my newsletter, I'm about to release a new series of training videos on Unity touching on hot topics.
Answer by FortisVenaliter · Apr 25, 2017 at 02:44 PM
If you're using C# (and it looks like you are), I recommend exorcising the 'var' keyword from your code. When 'var' is forbidden, code is much easier to debug, in my experience. Strong-typed languages can save you headaches!
Also, make sure to initialize your variables. Rather than just declaring them, give them a starting value, even if it's just zero. It increases code readability.
Thank you so much for saying this, --- incredibly important to maintain good code! Using 'var' is an abo$$anonymous$$ation in my opinion, haha! XD
Your answer

Follow this Question
Related Questions
if statements with no error when script not found 0 Answers
Enumaration error CS1025: Single-line comment or end-of-line expected 1 Answer
can anyone help me out what is wrong with this code? 1 Answer
If statement not working correctly 1 Answer
How apply something like the "!" Operator to an enum? 2 Answers