"foreach" and "}" and parsing error. Working on this code for my project.

voidStart ()
{

rig2d = GetComponent ();
anim = GetComponentInChildren ();

jmpForce = JumpForce;
GameObject[ ] players = GameObject.FindGameObjectsWithTag(“Player”)

foreach(GameObjectplinplayers)
{

if (pl.transform != this.transform)
{
enemy = pl.transform;
}
}
}

voidUpdate()
{
AttackInput();
ScaleCheck();
OnGroundCheck();
Damage();
UpdateAnimator();
}

voidOnGroundCheck()
{
if (!onGround)
{
rig2d.gravityScale = 5;
}
else
{
rig2d.gravityScale = 1;
}
}

voidFixedUpdate ()

{

horizontal = Input.GetAxis (“Horizontal” + PlayerNumber.ToString ());
vertical = Input.GetAxis(“Vertical” + PlayerNumber.ToString());

Vector3movement = newVector3(horizontal, 0, 0);

crouch = (vertical < -0.1f);

if (vertical > 0.1f)
{
if (!jumpKey)
{
jmpDuration += Time.deltaTime;
jmpForce += Time.deltaTime;

if (jmpDuration < jumpDuration)
{
rig2d.velocity = newVector2(rig2d.velocity.x, jmpForce);
}
else
{
jumpKey = true;
}
}
}

if(!onGround && vertical < 0.1f)
{
falling = true;
}

if(attack[0] && !jumpKey || attack[1] && !jumpKey)
{
movement = Vector3.zero;
}
if (!crouch)
rig2d.AddForce (movement * maxSpeed);
else
rig2d.velocity = Vector3.zero;
}

voidScaleCheck()
{
if(transform.position.x < enemy.position.x)
transform.localScale = newVector3(-1,1,1);
else
transform.localScale = Vector3.one;
}

voidAttackInput()
{
if(Input.GetButtonDown(“Attack1” + PlayerNumber.ToString()))
{
attack[0] = true;
attacktimer[0] = 0;
timesPressed[0] ++;
}

if(attack[0])
{
attacktimer[0] += Time.deltaTime;

if(attacktimer[0] > attackRate || timesPressed[0] >=4)
{
attacktimer[0] = 0;
attack[0] = false;
timesPressed [0] = 0;
}
}

if(Input.GetButtonDown(“Attack2” + PlayerNumber.ToString()))
{
attack[1] = true;
attacktimer[1] = 0;
timesPressed[1] ++;
}

if(attack[1])
{
attacktimer[1] += Time.deltaTime;

if(attacktimer[1] > attackRate || timesPressed[1] >=4)
{
attacktimer[1] = 0;
attack[1] = false;
timesPressed [1] = 0;
}
}
}

voidDamage()
{
{
noDamageTimer += Time.deltaTime;

if (noDamageTimer > noDamage)
{
damage = false;
noDamgaeTimer = 0;
}
}

//if(!onGround)
//{
//rig2d.gravityScale = 10;
//Vector3dir = enemy.position - transform.position;
//rig2d.AddForce(-dir * 25);
//}

}

voidUpdateAnimator()
{
anim.SetBool (“Crouch”, crouch);
anim.SetBool (“OnGround”, this.onGround);
anim.SetBool (“Falling”, this.falling);
anim.SetFloat (“Movement”, Mathf.Abs(horizontal));
anim.SetBool (“Attack1”, attack [0]);
anim.SetBool (“Attack2”, attack[1]);
}

voidOnCollisionEnter2D(Collision2Dcol)
{

if (col.collider.tag == “Ground”)
{
onGround = true;

jumpKey = false;
jmpDuration = 0;
jmpForce = JumpForce;
falling = false;
}

}

voidOnCollisionExit2D(Collision2Dcol)
{
if (col.collider.tag == “Ground”)
{
onGround = false;
}
}
}

  1. Use code tags
  2. The error message gives you a line number. We need that. We’re not going to read this whole thing line by line for you.
  1. He’s right, you need to use code tags properly.
  2. Didn’t have to read the whole thing. Ctl+F (or Cmd+F) works wonders. After your foreach loop you have one too many “}”.

This may not be true. It appears that he has an IF, a ForEach and a Start. That’s 3 opening braces and 3 closing.
It looks like the actual issue is this line:

GameObject[] players = GameObject.FindGameObjectsWithTag("Player")

has no line ending ;

Using proper code tags and giving us the error would help us identify the certainty.

7 Likes

Yes, found Waldo !

foreach is one set of { } and if is another set. So he should only have two closing } but he has 3. BUT, you’re right. If I re-read the thread’s title, he’s reporting two errors. So the first is the line ending, and the second is the one too many }'s.

1 Like

Void Start() is also a a set, which is what it appears his extra ending ‘}’ is closing out, as the next line is a Update() function.

1 Like

I also just realized we’re not even seeing the whole script file here…

Damn! There’s so much suspens in this thread, it could be a Night Shyamalan’s movie!!

In second look, I think I found the error for the extra }, so it was two separate errors. I wont attempt to verify this though until the beach has been cleared and Waldo is visible :slight_smile:

2 Likes

This?

Assets/Scripts/PlayerControl.cs(1,1): Error CS8025: Parsing error (CS8025) (Assembly-CSharp)

Assets/Scripts/PlayerControl.cs(8,8): Error CS1525: Unexpected symbol `}’ (CS1525) (Assembly-CSharp)

Assets/Scripts/PlayerControl.cs(8,8): Error CS1525: Unexpected symbol `}’ (CS1525) (Assembly-CSharp)

2413653--165037--Screen Shot 2015-12-08 at 11.50.25 AM.png

1 Like

Yes, that. Can you post the whole script here in tags so that we can get a better look? Also, did you fix the first error I pointed out?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerControl : MonoBehaviour
{
   
    public int PlayerNumber = 1;
    Transform enemy;
   
    Rigidbody2D rig2d;
    Animator anim;
   
    float horizontal;
    float vertical;
    public float maxSpeed = 25;
    Vector3 movement;
    bool crouch;
   
   
    public float JumpForce = 20;
    public float jumpDuration = .1f;
    float jmpDuration;
    float jmpForce;
    bool jumpKey;
    bool falling;
    bool onGround;
   
    public float attackRate = 0.3f;
        bool[] attack = new bool[2];
        float[] attacktimer = new float[2];
        int[] timesPressed = new int[2];

        public bool damage;
        public bool noDamage = 1;
        float noDamageTimer;
   
    void Start ()
    {
       
        rig2d = GetComponent<Rigidbody2D> ();
        anim = GetComponentInChildren<Animator> ();
       
        jmpForce = JumpForce;
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player")
       

            foreach(GameObject pl in players)
        {
       
            if (pl.transform != this.transform)
            {
                enemy = pl.transform;
            }
        }
    }
   
    void Update()
    {
        AttackInput();
        ScaleCheck();
        OnGroundCheck();
        Damage();
        UpdateAnimator();
    }
   

    void OnGroundCheck()
    {
        if (!onGround)
        {
            rig2d.gravityScale = 5;
        }
        else
        {
            rig2d.gravityScale = 1;
        }
    }
   
    void FixedUpdate ()
       
    {
       
        horizontal = Input.GetAxis ("Horizontal" + PlayerNumber.ToString ());
        vertical = Input.GetAxis("Vertical" + PlayerNumber.ToString());
       
        Vector3 movement = newVector3(horizontal, 0, 0);
       
        crouch = (vertical < -0.1f);
       
        if (vertical > 0.1f)
        {
            if (!jumpKey)
            {
                jmpDuration += Time.deltaTime;
                jmpForce += Time.deltaTime;
               
                if (jmpDuration < jumpDuration)
                {
                    rig2d.velocity = newVector2(rig2d.velocity.x, jmpForce);
                }
                else
                {
                    jumpKey = true;
                }
            }
        }
       
        if(!onGround && vertical < 0.1f)
        {
            falling = true;
        }

        if(attack[0] && !jumpKey || attack[1] && !jumpKey)
        {
            movement = Vector3.zero;
        }
        if (!crouch)
            rig2d.AddForce (movement * maxSpeed);
        else
            rig2d.velocity = Vector3.zero;
    }

    void ScaleCheck()
    {
        if(transform.position.x < enemy.position.x)
            transform.localScale = new Vector3(-1,1,1);
        else
            transform.localScale = Vector3.one;
    }

    void AttackInput()
    {
        if(Input.GetButtonDown("Attack1" + PlayerNumber.ToString()))
          {
            attack[0] = true;
            attacktimer[0] = 0;
            timesPressed[0] ++;
         }

        if(attack[0])
        {
            attacktimer[0] += Time.deltaTime;

            if(attacktimer[0] > attackRate || timesPressed[0] >=4)
            {
                attacktimer[0] = 0;
                attack[0] = false;
                timesPressed [0] = 0;
            }
        }
   

        if(Input.GetButtonDown("Attack2" + PlayerNumber.ToString()))
           {
            attack[1] = true;
            attacktimer[1] = 0;
            timesPressed[1] ++;
        }
       
        if(attack[1])
        {
            attacktimer[1] += Time.deltaTime;
           
            if(attacktimer[1] > attackRate || timesPressed[1] >=4)
            {
                attacktimer[1] = 0;
                attack[1] = false;
                timesPressed [1] = 0;
            }
        }
    }

    void Damage()
    {
        {
            noDamageTimer += Time.deltaTime;

            if (noDamageTimer > noDamage)
            {
                damage = false;
                noDamgaeTimer = 0;
            }
        }

        // if(!onGround)
        //{
            //rig2d.gravityScale = 10;
            //Vector3 dir = enemy.position - transform.position;
            //rig2d.AddForce(-dir * 25);
        //}

    }


    void UpdateAnimator()
    {
        anim.SetBool ("Crouch", crouch);
        anim.SetBool ("OnGround", this.onGround);
        anim.SetBool ("Falling", this.falling);
        anim.SetFloat ("Movement", Mathf.Abs(horizontal));
        anim.SetBool ("Attack1", attack [0]);
        anim.SetBool ("Attack2", attack[1]);
    }
   
    void OnCollisionEnter2D(Collision2D col)
    {

        if (col.collider.tag == "Ground")
        {
            onGround = true;
           
            jumpKey = false;
            jmpDuration = 0;
            jmpForce = JumpForce;
            falling = false;
        }
   
    }
   
    void OnCollisionExit2D(Collision2D col)
    {
        if (col.collider.tag == "Ground")
        {
            onGround = false;
        }
    }
}

Thanks., why don’t we start with giving line 45 a line ending. I’ll check it out a little more when I am home. After you do that change, are there any new/different errors?

1 Like

Now I have two errors after I closed line 45. They are both on line 58.

58 Assets/Scripts/PlayerControl.cs(9,9): Error CS1547: Keyword `void’ cannot be used in this context (CS1547) (Assembly-CSharp)

58 Assets/Scripts/PlayerControl.cs(19,19): Error CS1525: Unexpected symbol (', expecting ,‘, ;', or =’ (CS1525) (Assembly-CSharp)

I see errors (that I point out below), but not with the line you comment about. Are those error reports from within Unity or from your IDE?

Line 35:

public bool noDamage = 1;

1 is an int, not a bool. Reading further into your code, it actually looks like this should be a float.

Line 182:

noDamgaeTimer = 0;

spelling error. should be noDamageTimer.

Line 87 + 100:

Vector3 movement = newVector3(horizontal, 0, 0);

You need a space between ‘new’ and ‘Vector3’. On Line 100 it’s same error except with a new Vector2.

Fix these errors and let me know what happens :slight_smile:

Regards,
Rob

I fixed all the errors you talked about but I still have a problem with this code. Unity tells me that the void cannot be used in this context

void Update(){

In this case, when Unity says “in this context”, it most likely means that it sees that line as already being inside a function (and you can’t declare ‘void’ type variables within functions). This means that you still have mismatched curly brackets, and in this case it means that somewhere above “void Update” you are missing a }.