Hey all,
I am very new to coding and working on my first very simple game, however I have ran into a problem that i can’t quite fix any help you could give would be appreciated. The Error is as follows "CS0019: Operator ‘&&’ cannot be applied to operands of type ‘bool’ and ‘string’.
public void PawnMovePlate(int x, int y)
{
Game sc = Controller.GetComponent<Game>();
if (sc.PositionOnBoard(x, y))
{
if (sc.GetPosition(x, y) == null)
{
MovePlateSpawn(x, y);
}
if (sc.PositionOnBoard(x + 1, y) && sc.GetPosition(x+1, y) != null && sc.GetPosition (x+1, y).GetComponent<Chessman>().player! = player)
{
MovePlateAttackSpawn(x + 1, y);
}
if (sc.PositionOnBoard(x - 1, y) && sc.GetPosition(x-1, y) != null && sc.GetPosition(x-1, y).GetComponent<Chessman>().player! = player)
{
MovePlateAttackSpawn(x - 1, y);
}
}
}
If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.
How to break down hairy lines of code:
http://plbm.com/?p=248
Break it up, practice social distancing in your code, one thing per line please.
This problem is analogous to this:
Some help to fix “Cannot implicitly convert type ‘Xxxxx’ into ‘Yyyy’:”
http://plbm.com/?p=263
Reason about what is on the left and right sides of which ever && is giving you the error and ask yourself, “which one is not a bool” and fix that.
1 Like
This is not a valid operator: ! =
it has to be !=
. That’s not two operators but one, the not-equals operator. When you put a space in the middle they become seperate and do not make any sense in this context. The !
would be a unary “not” operator that has to placed before a boolean value. The standalone =
operator would be an assignment. Both do not make any sense here. However the compiler reads and analyzes the code top-to-bottom and left-to-right. So what it sees is your first boolean conditions followed by &&
followed by your “player name”. Since the just mentioned operators do not make any sense the first error when reading the code is that you try to “and” a boolean with a string.
1 Like