Unity C# problem

Associate
Joined
28 Jul 2014
Posts
694
Location
Liverpool
I am currently coding my 2D game for Android and ran into a parsing error along the way. I am coding in C# and I can not seem to get shut of this error, I have tried closing and opening the brackets and still nothing. Any help would be appreciated. Thanks.

Line (30/1) error CS8025: Parsing error

{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}


Code:
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

private Vector2 fp; // first finger position
private Vector2 lp; // last finger position

void Update () {
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
Vector3 newPosition = transform.position;
if((fp.x - lp.x) > 2) // left swipe
{
newPosition.x -=2;
transform.position = newPosition;
}
else if((fp.x - lp.x) < -2) // right swipe
{
newPosition.x +=2;
transform.position = newPosition;
}
}
}
}
}

{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}

// Die by being off screen
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}

// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
Die();
}

public void Die() {
Application.LoadLevel("gameover");

}
}
 
I don't code in c# and only have limited experience in other languages, but I count 12 { and 14 }

Having posted the code into sublime and just browsed through, it appears that the { on line 50 isn't paired:

Die();
}
}

and the very last } isn't paired either.
 
Line (30/1) error CS8025: Parsing error

{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}

What is that line supposed to do?

You cant just have an open brace on it's own, should it have an if() in front of it or something?
 
As AHarvey says, lots of the braces are not paired either.

This is what the code looks like indented properly:
Code:
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	private Vector2 fp; // first finger position
	private Vector2 lp; // last finger position

	void Update () 
	{
		foreach(Touch touch in Input.touches)
		{
			if (touch.phase == TouchPhase.Began)
			{
				fp = touch.position;
				lp = touch.position;
			}
			if (touch.phase == TouchPhase.Moved )
			{
				lp = touch.position;
			}
			if(touch.phase == TouchPhase.Ended)
			{
				Vector3 newPosition = transform.position;
				if((fp.x - lp.x) > 2) // left swipe
				{
					newPosition.x -=2;
					transform.position = newPosition;
				}
				else if((fp.x - lp.x) < -2) // right swipe
				{
					newPosition.x +=2;
					transform.position = newPosition;
				}
			}
		}
	}
}


{
	rigidbody2D.velocity = Vector2.zero;
	rigidbody2D.AddForce(jumpForce);
}

// Die by being off screen
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
	Die();
}
}

// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
	Die();
}

public void Die() 
{
	Application.LoadLevel("gameover");
}
}

Everything after the line which gives you the error seems to be in the wrong place. It should probably be inside the player class.
 
Assuming you're using Visual Studio, press CTRL K + D to have it format your code for you. Correct indentation make code so much easier to read.
 
It can't parse the object, you need to instantiate the instance variable 'jumpForce'.
Your miss-use of brackets will also generate a compile/runtime error.
Code:
private Vector2 jumpForce = new Vector2(0, 300);
//change 0,300 to desired value(s).

   Class x
   { //Within the class

    rigidbody2D.velocity = Vector2.zero;
    rigidbody2D.AddForce(jumpForce);
   
     }
 
I like 4, or a tab ;)

Wasn't aware there was even a debate on it heh.

It should always be 1 tab unless your coding in 1-2 languages where that formatting causes issues. Anything else is either not clear enough or takes up too much screen estate.
 
Back
Top Bottom