- Home /
Question about ?: (if/else) operators
I learned to use these operators only a few days ago so I'm still trying to figure out how to them exactly. Here's a problem I have though:
When I use it like this it works:
intName = condition
? 1
: 2
But when I try this it doesn't work
condition
? intName 1
: intName 2
While it doesn't matter in this situation cause I can just use the first one, it does matter in other situations. I'm currently trying to do this:
GameObject == null ? StartCoroutine(Name()) : doSomething();
The error I'm getting is the following :
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
Seen this error a lot but in this certain situation I don't know how to fix it. Thanks in advance :), also if anyone wants to tell me how to show my code better instead of typing it out here feel free too cause I'm kinda new to asking questions so I have no idea how everyone does that.
When you post something there is a bar above that gives several options (like the Bold you used). In the same bar there is an icon with some binary. Select your code you've written/pasted and click that icon.
Thanks ;p, Idk how it changed in my post though. I didn't do anything :O
Here is a workaround : https://stackoverflow.com/questions/5490095/method-call-using-ternary-operator
Answer by MNNoxMortem · Oct 23, 2017 at 10:21 AM
?: is not the very same as if/else, it is a conditional operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator). You can use it to assign the conditional values to some variable, but not as a if/else replacement when you do not have an assignment.
Yup. For example
true ? 5 : 10;
is an expression that has a value just like a variable has a value. This particular expression always has the value 5.
Writing that single line of code is not a valid statement just like it wouldn't be if you just wrote its real value
void Start() {
5;
} // Gives an error
Assigning a value however is a valid statement so you can write
int variable = 5;
// Or
int variable = true ? 5 : 10;
Damn, i misclicked. That should be a comment ins$$anonymous$$d of an answer. See Nose$$anonymous$$ills comment above. I was too lazy to explain it in depth and he did so.
Answer by AlbertoFdzM · Oct 23, 2017 at 02:39 PM
That way to use if/else is called "ternary operator". It's made to be used in expressions that have to return some value. In other words, whenever you use a ternary operator you are telling to the program that you are going to return something from that expression. So, for example, the line true == true ? 1 : 2
is going to return 1
when executed. C# doesn't allow expresions that returns something if you'r not assigning the returned value to any variable. if you do this int myVal = true == true ? 1 : 2
the expression will be executed correctly and myVal
will have a value of 1
. That's the difference between the regular if/else declaration and the ternary operator.
Your answer
Follow this Question
Related Questions
How can I overload operators in Unity Javascript 1 Answer
< cheaper than <= ? 2 Answers
Trouble with understanding code that uses "==" operator. 2 Answers
How do use the % operator? 2 Answers