Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Caramelkisses · Apr 11, 2013 at 11:57 PM · 2dif-statementsside-scrolling

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loius · Apr 12, 2013 at 12:01 AM 1
Share

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.

avatar image Caramelkisses · Apr 13, 2013 at 04:32 PM 0
Share

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.

avatar image robertbu · Apr 14, 2013 at 02:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

"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

Isolate z,y Movement For 2d Side Scroller 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges