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

Bug New to coding! Something is wrong with my script...

Discussion in 'Scripting' started by abbyhoffmann, Jun 11, 2023.

  1. abbyhoffmann

    abbyhoffmann

    Joined:
    Jun 11, 2023
    Posts:
    1
    I am following a tutorial on YouTube and have written everything exactly but I am unsure of what the error is! I have three errors:

    - Assets\PlayerController.cs(34,50): error CS1526: A new expression requires an argument list or (), [], or {} after type
    - Assets\PlayerController.cs(34,50): error CS1003: Syntax error, ',' expected
    - Assets\PlayerController.cs(43,9): error CS0106: The modifier 'private' is not valid for this item


    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. // Takes and handles input and movement for a player character
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     public float moveSpeed = 1f;
    10.     public float collisionOffset = 0.05f;
    11.     public ContactFilter2D movementFilter;
    12.  
    13.     Vector2 movementInput;
    14.     Rigidbody2D rb;
    15.     Animator animator;
    16.     List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         rb = GetComponent<Rigidbody2D>();  
    22.         animator - GetComponent<Animator>();
    23.     }
    24.  
    25.     private void FixedUpdate() {
    26.         // If movement input is not 0, try to move
    27.         if(movementInput != Vector2.zero){
    28.             bool success = TryMove(movementInput);
    29.  
    30.             if(!success) {
    31.                 success = TryMove(new Vector2(movementInput.x, 0));
    32.  
    33.                 if(!success) {
    34.                     success = TryMove(new Vector 2(0, movementInput.y));
    35.                 }
    36.             }
    37.  
    38.             animator.SetBool("isMoving", success);
    39.         }   else {
    40.                 animator.SetBool("isMoving", false);
    41.     }
    42.  
    43.         private bool TryMove(Vector2 direction) {
    44.             // Check for potential collisions
    45.             int count = rb.Cast(
    46.                 direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
    47.                 movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
    48.                 castCollisions, // List of collisions to store the found collisions into after the Cast is finished
    49.                 moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
    50.  
    51.             if(count == 0){
    52.                 rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
    53.                 return true;
    54.             } else {
    55.                 return false;
    56.             }
    57.         }
    58.     }
    59.  
    60.     void OnMove(InputValue movementValue) {
    61.         movementInput = movementValue.Get<Vector2>();
    62.     }
    63.  
    64. }
     
  2. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    44
    Hi,

    I think there is a problem in your code brackets : you're declaring the TryMove function inside the FixedUpdate function.
    You can solve this by adding a closing bracket before the TryMove function, to close the FixedUpdate.
    Try to always have your code with the correct indentation - you can use formatter to get it right for you; it will help you see if there is something wrong like in this case
     
    Bunny83 and mopthrow like this.
  3. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Well, it's a few days old but in case it's not fixed yet; in addition to what @Reaktion has said, you have a couple of typos:

    Line 34
    Vector 2
    should be
    Vector2
    (remove the space)
    Line 22 the - should be an =
     
    Reaktion and Bunny83 like this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    Right, that's why I really don't like this old C style bracket placement. In C# the default coding style guide recommends to place opening brackets on a new line. It makes it much easier to see which brackets belong together as they have to be at the exact same indention level. I just inserted the code in Visual Studio with my normal code style settings and it reformatted the code to:

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.InputSystem;
    5.  
    6.     // Takes and handles input and movement for a player character
    7.     public class PlayerController : MonoBehaviour
    8.     {
    9.         public float moveSpeed = 1f;
    10.         public float collisionOffset = 0.05f;
    11.         public ContactFilter2D movementFilter;
    12.  
    13.         Vector2 movementInput;
    14.         Rigidbody2D rb;
    15.         Animator animator;
    16.         List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
    17.  
    18.         // Start is called before the first frame update
    19.         void Start()
    20.         {
    21.             rb = GetComponent<Rigidbody2D>();
    22.             animator - GetComponent<Animator>();
    23.         }
    24.  
    25.         private void FixedUpdate()
    26.         {
    27.             // If movement input is not 0, try to move
    28.             if (movementInput != Vector2.zero)
    29.             {
    30.                 bool success = TryMove(movementInput);
    31.  
    32.                 if (!success)
    33.                 {
    34.                     success = TryMove(new Vector2(movementInput.x, 0));
    35.  
    36.                     if (!success)
    37.                     {
    38.                         success = TryMove(new Vector2(0, movementInput.y));
    39.                     }
    40.                 }
    41.  
    42.                 animator.SetBool("isMoving", success);
    43.             }
    44.             else
    45.             {
    46.                 animator.SetBool("isMoving", false);
    47.             }
    48.  
    49.             private bool TryMove(Vector2 direction)
    50.             {
    51.                 // Check for potential collisions
    52.                 int count = rb.Cast(
    53.                     direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
    54.                     movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
    55.                     castCollisions, // List of collisions to store the found collisions into after the Cast is finished
    56.                     moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
    57.  
    58.                 if (count == 0)
    59.                 {
    60.                     rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
    61.                     return true;
    62.                 }
    63.                 else
    64.                 {
    65.                     return false;
    66.                 }
    67.             }
    68.         }
    69.  
    70.         void OnMove(InputValue movementValue)
    71.         {
    72.             movementInput = movementValue.Get<Vector2>();
    73.         }
    74.     }
    75.  
    I fixed the
    Vector 2
    mistake but I didn't move the TryMove method out of the FixedUpdate method to show the actual nesting properly.

    In theory you could keep the method inside FixedUpdate as local methods are now a thing. However if you do you can not use the private keyword as this only works for members of a class or struct.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Nope! As you can tell, you have errors. You will know when you have "written everything exactly" because there will be no errors.

    Try this simple two-step process. I've never seen it fail:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    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.

    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.

    Your IDE should be giving you typing guidance. If not, here's how to fix it:

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874