1

I am getting an error when trying to use an one line if statement in Unity. I can't figure out what is wrong with it. Here is the line: MyImage.color == Color.white ? MyImage.color = Color.green : MyImage.color = Color.white;

I have it in the Update function. It's all that is in the update function. It says only assignment, call, increment, decrement, await, and new object expressions can be used as a statement. I am assigning a color to an image so I'm not sure what is wrong. This is unity 2018.3.0f3 if that matters.

1 Answers1

4

the correct way should be like this:

MyImage.color = (MyImage.color == Color.white) ? Color.green : Color.white;
derHugo
  • 83,094
  • 9
  • 75
  • 115
imleoson
  • 142
  • 11
  • 1
    The `( )` are redundant, though – derHugo Sep 04 '19 at 09:12
  • 1
    I tried to make it readable and easy to understand, instead of long explanation. and agree, it's not necessary to have "( )". – imleoson Sep 04 '19 at 10:38
  • 1
    Oh if you have one please add a long explanation! we want quality posts here and the more valid information the better ;) – derHugo Sep 04 '19 at 11:01
  • it's already very clean and readable. it's good enough to solve the problem without pages of theory, which are not necessary. – imleoson Sep 04 '19 at 11:21
  • 1
    well more or less ... currently it is a code snippet that eventually works ... but what exactly the original issue was is not totally cleared here (`only assignment, call, increment, decrement, await, and new object expressions can be used as a statement` .. apparently OP didn't understand what this message really means) – derHugo Sep 04 '19 at 11:25
  • Interesting, it depends on what people is looking for. I simply provide a working solution because I think that he had syntax error. In this case, I don’t understand the point of explaining in long story... – imleoson Sep 04 '19 at 11:48