- Home /
Value between two states
In know it is pretty basic question but I could not find best solution for this. I want value between two state for example 1 or -1. No other value allowed for this.
I know using condition but I want unity way of doing this. Please expert provide some suggestion in this.
Answer by Bunny83 · Apr 14, 2014 at 11:20 AM
Your question is lacking some important information. fafase actually answered the question "as it is" since you didn't mention that you want to randomly choose one of the "states".
Anyways if you want to generate randomly either (1) or (-1) you just do this:
var val = Random.Range(0,2)*2-1;
Random.Range(0,2) (!!with integer values!!) returns either 0 or 1
You multiply the result by 2 so the value is 0 or 2
subtract 1 so the 0 becomes -1 and the 2 becomes 1
Elementary school math ;)
Answer by fafase · Apr 14, 2014 at 10:41 AM
You could use boolean, and then convert to an integer:
bool value;
int result;
result = value ? 1 : -1;
other way more explicit is using enum:
public enum State{No = -1, Yes = 1}
State state = State.None;
state = something ? State.Yes:State.No;
int result = (int)state;
What about bool? If I can able to generate true or false randomly then there is no need to define -1 or 1. I think now you understand my question.
I want to increment row index by +1 or -1 based on randomization. But how to get either +1 or -1? Value between that only.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142655im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In sequence position game objects 1 Answer
Game Development Approach 0 Answers
GameObjects creation within boundry 3 Answers
"Send Message Has No Receiver" On Button Click 1 Answer
Apply Constant Force 2 Answers