- Home /
C# Incrementing an variable till it reaches a certain amount?
Hi everyone, is it possible to increment a variable till it reaches a certain amount? I know this can be done with if statements but I'm wondering if it can be done with just the incremental operator. I"m guessing it would look something like this. ++somevar till 40;
Comment
Best Answer
Answer by whydoidoit · Apr 28, 2013 at 10:08 PM
No you need to use if or Mathf.Clamp. You can use the ternary operator too:
i = i < 40 ? i+1 : i;
I tried that but my number isn't incrementing.How would you do it with a mathf.clamp?
You could also create an extension function that would do something like:
i.IncrementTo(40);