Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Get this error Assets\johnsa.cs(62,26): error CS1002: ; expected Any help?

Discussion in 'Scripting' started by yanferstudios, Aug 3, 2023.

  1. yanferstudios

    yanferstudios

    Joined:
    Aug 3, 2023
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class johnsa : MonoBehaviour
    {
    public GameObject BulletPrefab;
    public float Speed;
    public float JumpForce;

    private Rigidbody2D Rigidbody2D;
    private Animator Animator;
    private float horizontal;
    private bool Grounded;
    void Start()
    {
    Rigidbody2D = GetComponent<Rigidbody2D>();
    Animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    horizontal = Input.GetAxisRaw("Horizontal");

    if (horizontal < 0.0f) transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
    else if (horizontal > 0.0f) transform.localScale = new Vector3(1.0f, 1.0f);


    Animator.SetBool("running", horizontal != 0.0f);

    Debug.DrawRay(transform.position, Vector3.down * 0.1f, Color.red);

    if (Physics2D.Raycast(transform.position, Vector3.down, 0.1f))
    {
    Grounded = true;
    }
    else Grounded = false;
    if (Input.GetKeyDown("w") && Grounded)
    {
    Jump();
    }
    if (Input.GetKey("space"))


    Shoot();

    }

    private void Jump ()
    {
    Rigidbody2D.AddForce(Vector2.up * JumpForce);
    }

    private void Shoot()

    {
    Vector3 direction;
    if (transform.localScale.x == 1.0f) direction = Vector2.right;
    else direction = Vector3.left;

    GameObject bullet Instantiate(BulletPrefab, transform.position + direction * 0.1f, Quaternion.identity);
    bullet.GetComponent<BulletScript>().SetDirection(direction);
    }
    private void FixedUpdate()
    {
    Rigidbody2D.velocity = new Vector2(horizontal, Rigidbody2D.velocity.y);
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Use code tags!
    The error says, line 62, char 26.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class johnsa : MonoBehaviour
    6. {
    7. public GameObject BulletPrefab;
    8. public float Speed;
    9. public float JumpForce;
    10.  
    11. private Rigidbody2D Rigidbody2D;
    12. private Animator Animator;
    13. private float horizontal;
    14. private bool Grounded;
    15. void Start()
    16. {
    17. Rigidbody2D = GetComponent<Rigidbody2D>();
    18. Animator = GetComponent<Animator>();
    19. }
    20.  
    21. // Update is called once per frame
    22. void Update()
    23. {
    24. horizontal = Input.GetAxisRaw("Horizontal");
    25.  
    26. if (horizontal < 0.0f) transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
    27. else if (horizontal > 0.0f) transform.localScale = new Vector3(1.0f, 1.0f);
    28.  
    29.  
    30. Animator.SetBool("running", horizontal != 0.0f);
    31.  
    32. Debug.DrawRay(transform.position, Vector3.down * 0.1f, Color.red);
    33.  
    34. if (Physics2D.Raycast(transform.position, Vector3.down, 0.1f))
    35. {
    36. Grounded = true;
    37. }
    38. else Grounded = false;
    39. if (Input.GetKeyDown("w") && Grounded)
    40. {
    41. Jump();
    42. }
    43. if (Input.GetKey("space"))
    44.  
    45.  
    46. Shoot();
    47.  
    48. }
    49.  
    50. private void Jump ()
    51. {
    52. Rigidbody2D.AddForce(Vector2.up * JumpForce);
    53. }
    54.  
    55. private void Shoot()
    56.  
    57. {
    58. Vector3 direction;
    59. if (transform.localScale.x == 1.0f) direction = Vector2.right;
    60. else direction = Vector3.left;
    61.  
    62. GameObject bullet Instantiate(BulletPrefab, transform.position + direction * 0.1f, Quaternion.identity);
    63. bullet.GetComponent<BulletScript>().SetDirection(direction);
    64. }
    65. private void FixedUpdate()
    66. {
    67. Rigidbody2D.velocity = new Vector2(horizontal, Rigidbody2D.velocity.y);
    68. }
    69. }
    However, this can be a weird error because it doesn't always mean you're missing a ; just that it thinks you are. In this case, look at line 62 and read it through. Hint, you're missing an =
     
    yanferstudios likes this.
  3. yanferstudios

    yanferstudios

    Joined:
    Aug 3, 2023
    Posts:
    4
    thanks i put GameObject bullet Instantiate instead of GameObject bullet = Instantiate
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    As I already told you here:

    https://forum.unity.com/threads/cha...till-struggling-here-can-anyone-help.1468532/

    You must have zero typos. When you get one, GO FIX IT. Don't post here.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.