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.

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;

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 :wink: