Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by James_Sweetlove · Jan 23, 2018 at 08:40 PM · errorifif statementunexpected-symbol

My "if" statement doesn't work?

Hi there, i am new to the community an am trying to write a bit of code for an online lesson! so far i have managed to fix pretty much all of my errors i just can't figure this one out. Could someone tell me where about i am going wrong please?

here is the code:

 void Start () {
     
 }
 
 // Update is called once per frame
 void Update (){
     var rigidbody =
         GetComponent<Rigidbody2D> ();
     var transform =
         GetComponent<Transform> ();
           if (transform.position.y < -6) {
          (transform.position = new Vector2
             (-5,2)
         )
         if (Input.GetKey ("right")) {
             rigidbody.velocity = new Vector2 (5, 0);
     Debug.Log ("The right arrow was pressed");
 }

     if (Input.GetKey ("left")) {
             rigidbody.velocity = new Vector2 (-5, 0);
         Debug.Log ("The left arrow was pressed");
     }             
     if (Input.GetKey ("space")) {
         rigidbody.velocity = new Vector2 (0, 5);
                 Debug.Log ("The space button was pressed");
             }
         }
     }
 }

Thank you, i appreciate anyone response. Sorry if its really simple ;-;

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LTonon · Jan 23, 2018 at 08:55 PM

  void Update ()
 {
      var rigidbody = GetComponent<Rigidbody2D> ();
      var transform = GetComponent<Transform> ();
       
      if (transform.position.y < -6) 
      {
           (transform.position = new Vector2(-5, 2))
          
           if (Input.GetKey ("right")) 
           {
                rigidbody.velocity = new Vector2 (5, 0);
           
                Debug.Log ("The right arrow was pressed");
           }
      
           if (Input.GetKey ("left")) 
           {
                rigidbody.velocity = new Vector2 (-5, 0);
                Debug.Log ("The left arrow was pressed");
           }             
      
           if (Input.GetKey ("space")) 
           {
                rigidbody.velocity = new Vector2 (0, 5);
                Debug.Log ("The space button was pressed");
           }
      }
 }


Sorry, I couldn't read it correctly using the way you wrote it. Anyway, looking at it (and I copied exactly as you wrote), it seems that right after you do the comparison "transform.position.y < -6f" you do "(transform.position = new Vector2(-5,2))"... But this is not correct. First of all, don't use parenthesis on an entire line, and you also forgot about the ; at the end of the line.

So basically you need to do: transform.position = new Vector2(-5, 2);


As a good practice advice, avoid declaring variables and using GetComponent inside of the Update function, it may cause problems of performance. Besides, transform is already accessible through script without you needing to declare and access the component manually, so the better version of this would be:

 var rigidbody;
 
 void Start()
 {
      rigidbody = GetComponent<RigidBody>();
 }
 
 
Comment
Add comment · Show 7 · 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 James_Sweetlove · Jan 23, 2018 at 09:09 PM 1
Share

Thank you so much, this helps massively. I have said before but i am new and i really appreciate your help :)

avatar image LTonon James_Sweetlove · Jan 23, 2018 at 09:11 PM 0
Share

No problem! It worked? With practice program$$anonymous$$g gets easier... Well, not so much, but you get better at finding errors XD

avatar image James_Sweetlove · Jan 23, 2018 at 09:16 PM 0
Share

Sorry about this but i have just tried to input this into unity but it is still co$$anonymous$$g up with the parse error for the "if" statement on line "if (Input.Get$$anonymous$$ey ("right")) { rigidbody.velocity = new Vector2 (5, 0);

             Debug.Log ("The right arrow was pressed");
         }"

Any ideas?

I am also getting a message that says "unexpected symbol 'end of file"?

avatar image LTonon James_Sweetlove · Jan 23, 2018 at 09:18 PM 0
Share

Could you take a screenshot of the error message on the console and post it here?

avatar image James_Sweetlove · Jan 23, 2018 at 09:23 PM 0
Share

alt text

screen-shot-2018-01-23-at-212224.png (174.1 kB)
avatar image GamerGurkeLP James_Sweetlove · Jan 23, 2018 at 09:24 PM 0
Share

Can it be that the indet space is not correct so you have to remove one space before the if?

avatar image James_Sweetlove GamerGurkeLP · Jan 23, 2018 at 09:26 PM 0
Share

Doesn't seem to do anything ;-;

avatar image
0

Answer by James_Sweetlove · Jan 23, 2018 at 09:24 PM

alt text


screen-shot-2018-01-23-at-212420.png (35.7 kB)
Comment
Add comment · Show 9 · 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 LTonon · Jan 23, 2018 at 09:32 PM 0
Share

Ok, it seems that you have an error before the line 22 of your script, probably a missing bracket or something of the sort. Could you copy and paste it here exactly as you did on Unity?

avatar image James_Sweetlove LTonon · Jan 23, 2018 at 09:35 PM 0
Share

Sure,

 void Update (){

         var rigidbody = GetComponent<Rigidbody2D> ();
     var transform = GetComponent<Transform> ();

     if (transform.position.y < -6) 
     {
         (transform.position = new Vector2(-5, 2))

         if (Input.Get$$anonymous$$ey ("right")) 
             rigidbody.velocity = new Vector2 (5, 0);

             Debug.Log ("The right arrow was pressed");

         if (Input.Get$$anonymous$$ey ("left")) 
         {
             rigidbody.velocity = new Vector2 (-5, 0);
             Debug.Log ("The left arrow was pressed");
         }             

         if (Input.Get$$anonymous$$ey ("space")) 
         {
             rigidbody.velocity = new Vector2 (0, 5);
             Debug.Log ("The space button was pressed");
         }
     }
 }

}

please ignore the 4th "}" at the end. i know this is bad code language but it gets rid of the unexpected end of file error i keep receiving ;-;

avatar image James_Sweetlove James_Sweetlove · Jan 23, 2018 at 09:39 PM 0
Share

I have just had a look at it and i was actually missing a semicolon. but now it comes up with this?

Show more comments
Show more comments

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

100 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

HELP!! unexpected token: If 1 Answer

Add changeable conditions in the editor, similar to unityEvent? 0 Answers

Error CS1519 help 1 Answer

C# unexpected symbol error 1 Answer

The If statement condition is false but the if statement stills executes 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