-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); } } 
3
  • 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. Commented Jan 12, 2021 at 11:28
  • 1
    format your *.cs file - stackoverflow.com/questions/5755942/… - and than fix your - mostlikely - syntax error Commented Jan 12, 2021 at 11:32
  • please use the correct tags ... unityscript 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# Commented Mar 22, 2021 at 11:49

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

The @ looks like a PHP error supression feature (I just knew it thanks to CodeCaster's comment in the question)
@Cleptus Not familiar with PHP so I'll take your word for it ;o)
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.

1 Comment

Not sure if Unity supports it, not my ground, but nested functions got introduced in .net local functions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.