-4

I am making a 2D game in Unity , for the first time. I'm writing a script, also for the first time, I watched some tutorials and I think it's not bad. However, I keep getting an error message and I have no idea what to do. Well, the error is displayed to me in a place where previously no error was displayed and everything worked. Only after typing in "void Update" :

if (Input.GetKeyDown(KeyCode.R))

{
    Attack();
}


void Attack()

{
// Play an attack animation
animator.SetTrigger("Attack");
// Detect enemies in range of attack
// Damage them
}
} 

(I wrote it the way the tutorial said to) Suddenly when I wanted to see if it worked I got an error "Identifier expected". The error is located a line further down, here:

@if (grouned)
doubleJump = false;
anim.SetBool ("Grounded", grouned);


if(Input.GetKeyDown(KeyCode.W)&& grouned)

"If" displayed that something was wrong, so I added an "@" there, but now it shows that something should be/something is wrong after the word "grouned". I have no idea what to do.

Here you have the whole script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Player : MonoBehaviour

{

    public Animator animator;
    public float moveSpeed;
    public float jumpHeight;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask WhatIsGround;
    private bool grouned; 
    private bool doubleJump;
    
    private Animator anim;



    // Start is called before the first frame update
    void Start(){
    anim = GetComponent<Animator> ();
        
    }
    

    void FixedUpdate(){

    grouned = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, WhatIsGround);



}

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.R))
    {
        Attack();
    }
    

    void Attack()

    {
    // Play an attack animation
    animator.SetTrigger("Attack");
    // Detect enemies in range of attack
    // Damage them
    }
    } 

    @if (grouned)
    doubleJump = false;
    anim.SetBool ("Grounded", grouned);


    if(Input.GetKeyDown(KeyCode.W)&& grouned)

    
    {
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, jumpHeight);
        }


    if(Input.GetKeyDown(KeyCode.W)&& !grouned && !doubleJump)

    
    {
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, jumpHeight);
    doubleJump = true;
        }
    

    if(Input.GetKey(KeyCode.D))
    
    {
    GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }


    if(Input.GetKey(KeyCode.A))
    
    {
    GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }

    anim.SetFloat ("Speed", Mathf.Abs (GetComponent<Rigidbody2D> ().velocity.x));
    if(GetComponent<Rigidbody2D>().velocity.x > 0)

    {
    transform.localScale = new Vector3 (3f, 3f, 3f);
    }

    else if (GetComponent<Rigidbody2D>().velocity.x < 0)
    transform.localScale = new Vector3 (-3f, 3f, 3f);

}

    
    
    }
derHugo
  • 83,094
  • 9
  • 75
  • 115
Agatha
  • 1
  • 1
  • 4
    Your indentation is a mess, and that causes problems like this. You probably have some extraneous curly braces somewhere. Also, _""If" displayed that something was wrong, so I added an "@" there"_- why? This isn't PHP where you can suppress runtime errors with the @, you have a compiler error. Edit: yeah, your `void Update() { ... }` is missing its closing `}`, while the `void Attack() { ... }` has one to spare. – CodeCaster Jan 12 '21 at 11:28
  • 1
    format your *.cs file - https://stackoverflow.com/questions/5755942/how-do-you-auto-format-code-in-visual-studio - and than fix your - mostlikely - syntax error – Rand Random Jan 12 '21 at 11:32
  • please use the correct tags ... [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long deprecated by now ... your code is clearly `c#` – derHugo Mar 22 '21 at 11:49

2 Answers2

1

Having tidied up your code, it's obvious that the code starting at @if... is not contained within a method.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public Animator animator;
    public float moveSpeed;
    public float jumpHeight;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask WhatIsGround;
    private bool grouned;
    private bool doubleJump;

    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        grouned = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, WhatIsGround);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            Attack();
        }

        void Attack()
        {
            // Play an attack animation
            animator.SetTrigger("Attack");
            // Detect enemies in range of attack
            // Damage them
        }
    }

    @if(grouned)
        doubleJump = false;
        
    anim.SetBool("Grounded", grouned);

    if(Input.GetKeyDown(KeyCode.W)&& grouned)
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
    }

    if (Input.GetKeyDown(KeyCode.W) && !grouned && !doubleJump)
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
        doubleJump = true;
    }
    
    if (Input.GetKey(KeyCode.D))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
    }
    
    if (Input.GetKey(KeyCode.A))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
    }
    
    anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
    if (GetComponent<Rigidbody2D>().velocity.x > 0)
    {
        transform.localScale = new Vector3(3f, 3f, 3f);
    }
    else if (GetComponent<Rigidbody2D>().velocity.x < 0)
        transform.localScale = new Vector3(-3f, 3f, 3f);
    }
}

The chances are that some of your closing braces } are in the wrong place.

Keeping your code well formatted would have helped you identify this error. Good formatting is not just good practice, it helps with reading the code and also helps spot errors like this.

Also, I'm not sure what you were hoping to achieve with @if, this isn't a razor view/page!

phuzi
  • 12,078
  • 3
  • 26
  • 50
1

It looks like you've put your Attack function in the middle of the update function which is making your curly braces not line up properly and leaving a lot of code outside of a function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour

{

    public Animator animator;
    public float moveSpeed;
    public float jumpHeight;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask WhatIsGround;
    private bool grouned;
    private bool doubleJump;

    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        grouned = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, WhatIsGround);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            Attack();
        }

        if(grouned)
            doubleJump = false;
        anim.SetBool("Grounded", grouned);

        if (Input.GetKeyDown(KeyCode.W) && grouned)
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
        }
        if (Input.GetKeyDown(KeyCode.W) && !grouned && !doubleJump)
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
            doubleJump = true;
        }
        if (Input.GetKey(KeyCode.D))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }
        if (Input.GetKey(KeyCode.A))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }
        anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
        if (GetComponent<Rigidbody2D>().velocity.x > 0)
        {
            transform.localScale = new Vector3(3f, 3f, 3f);
        }
        else if (GetComponent<Rigidbody2D>().velocity.x < 0)
            transform.localScale = new Vector3(-3f, 3f, 3f);
    }

    void Attack()
    {
        // Play an attack animation
        animator.SetTrigger("Attack");
        // Detect enemies in range of attac
        // Damage them
    }
}

Are you using some kind of editor with syntax highlighting? It would help you catch errors like this along with indenting your code properly which will help you spot missing/extra { braces.

Arianne
  • 507
  • 2
  • 7
  • Not sure if Unity supports it, not my ground, but nested functions got introduced in .net [local functions](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions) – Cleptus Jan 12 '21 at 11:55