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 ;-;
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>();
}
Thank you so much, this helps massively. I have said before but i am new and i really appreciate your help :)
No problem! It worked? With practice program$$anonymous$$g gets easier... Well, not so much, but you get better at finding errors XD
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"?
Could you take a screenshot of the error message on the console and post it here?
Can it be that the indet space is not correct so you have to remove one space before the if?
Answer by James_Sweetlove · Jan 23, 2018 at 09:24 PM
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?
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 ;-;
I have just had a look at it and i was actually missing a semicolon. but now it comes up with this?