- Home /
Errors when using multiple if's in a statement.
Hi, I'm making a 2D game in which you play as a young girl fighting away ghosts. I've been toying with this code for a while, and I can't seem to make it work. The aim of the code is to move the ghost away from the character, making them fly away from the girl making sure that the ghost is close enough to the player.
static var Distance : float;
static var Direction : int;
static var WhichSide : String;
//ASSIGNS VALUES TO VARIABLES,
function Update () {
Distance = (Insanity.dist); //Any given distance, usually between 1 & 5
Direction = (MaterialChanger.CurrentDirection); // 1= Left 2=Right
WhichSide = (Insanity.CurrentSide); //"Left" or "Right"
//CHECKS IS VALUES ARE VIABLE FOR PLAYER ATTACK
if (Input.GetKeyDown(KeyCode.Attack1))
{
if ((Distance < 1) &&
(Direction = 1) &&
(WhichSide = "Left"))
{
LeftAttack();
}
if ((Distance < 1) &&
(Direction = 2) &&
(WhichSide = "Right"))
{
RightAttack();
}
}
}
//EXECUTES LEFT ATTACK
function LeftAttack()
{
rigidbody.AddForce (-1, 0.5, 0);
//print ("LEFT ATTACK ACHIEVED!");
}
function RightAttack()
{
rigidbody.AddForce (1, 0.5, 0);
//print ("RIGHT ATTACK ACHIEVED!");
}
Here are the errors;
Assets/Attacking.js(16,23): BCE0044: expecting ), found '='. Assets/Attacking.js(16,25): BCE0044: expecting ), found '1'. Assets/Attacking.js(16,26): BCE0043: Unexpected token: ). Assets/Attacking.js(17,23): BCE0044: expecting ), found '='. Assets/Attacking.js(17,24): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Attacking.js(17,25): BCE0043: Unexpected token: Left. Assets/Attacking.js(19,24): BCE0044: expecting :, found ';'.
I'm probably wrong, but I don't feel as though the errors displayed match the problems with the code.
Any help is much appreciated, thanks in advance!
Answer by robertbu · Apr 11, 2013 at 11:59 PM
Usually the first error is the most accurate, and then everything goes downhill from there. One issue that jumps out is that you are not using '==' for comparison on lines 16, 17, 23 and 24. A single '=' is used for assignment.
Everything goes downhill from there, hits an office building, and katamari's its way into a separate planetoid which upsets Earth's delicate balance and sends us spiraling into the sun.
In my experience.
Thanks! The "==" seems so obvious now. It stopped the errors occuring, but sadly the code didn't seem to do anything at all. So I tried re-writing the code, so that it wouldn't check as many values at a single time, this is the code I came up with:
static var Distance : float;
static var Direction : int;
static var WithinRange = false;
static var EnemyXAxis : float;
static var GirlXAxis : float;
static var CurrentSide : String;
//ASSIGNS VALUES TO VARIABLES,
function Update () {
XaxisLocation(); //Posistion of the targets
Distance = (Insanity.dist); //Any given distance, usually between 1 & 5
Direction = ($$anonymous$$aterialChanger.CurrentDirection); // 1= Left 2=Right
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
CheckDistance();
}
}
//CHEC$$anonymous$$S DISTANCE AND IF WITHIN RANGE
function CheckDistance(){
if (Distance < 5) {
WithinRange = true;
WhichSide();
}
}
// DECIDES WHETHER THE ENE$$anonymous$$Y IS ON THE LEFT OF RIGHT SIDE OF THE GIRL
function WhichSide()
{
if (EnemyXAxis < GirlXAxis){
CurrentSide = "Left";
CheckFacing();
}
else if (EnemyXAxis > GirlXAxis){
CurrentSide = "Right";
CheckFacing();
}
}
//CHEC$$anonymous$$S THAT THE PLAYER IS FACING THE ENE$$anonymous$$Y BEFORE ATTAC$$anonymous$$ING
function CheckFacing(){
if ((CurrentSide == "Left") && (Direction == 1))
{
LeftAttack();
}
else if ((CurrentSide == "Right") && (Direction == 2))
{
RightAttack();
}
}
//EXECUTES ATTAC$$anonymous$$S
function LeftAttack()
{
rigidbody.AddForce (-1, 0.5, 0);
print ("LEFT ATTAC$$anonymous$$ ACHIEVED!");
}
function RightAttack()
{
rigidbody.AddForce (1, 0.5, 0);
print ("RIGHT ATTAC$$anonymous$$ ACHIEVED!");
}
// FINDS THE POSISTION OF THE ENE$$anonymous$$Y ON THE "X" AXIS
function XaxisLocation()
{
EnemyXAxis = (Enemy$$anonymous$$ovement2.EnemyPos.x);
//print ("Enemy x axis value " + EnemyXAxis);
GirlXAxis = (Character$$anonymous$$over.playerPos.x);
// print ("Girl x axis value " + GirlXAxis);
}
//print ("Which side is the enemy on? The " + CurrentSide + " side"); 0
//OLD CODE
/* ((Distance < 1) &&
(Direction == 1) &&
(WhichSide == "Left"))
{
LeftAttack();
}
if ((Distance < 1) &&
(Direction == 2) &&
(WhichSide == "Right"))
{
RightAttack();
}
*/
The code is probably a bit sloppy, but it was only a quick write up just top see if it would work, and it hasn't. Could you any any reason why it doesn't? Thank you very much for all your help.
If you character has the default mass of 1.0, the amount of force you are applying on lines 58 and 63 won't do much. $$anonymous$$ultiply all your values by 500 or 1000.
Your answer

Follow this Question
Related Questions
"Restricting" a GameObject to just X & Y axis 4 Answers
2D Sprite Characters in 3D World and Locked Camera 1 Answer
GamePlay Question 2 Answers