Search Unity

Question Can't solve this error..I need help.."Type or namespace definition, or end-of-file expected"

Discussion in 'Scripting' started by Wolf_Devp, Jan 14, 2024.

  1. Wolf_Devp

    Wolf_Devp

    Joined:
    Jan 14, 2024
    Posts:
    7
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour{  
    6.  
    7.      [SerializeField] private float moveSpeed= 7f;
    8.      [SerializeField] private GameInput gameInput;
    9.      
    10.       private bool isWalking;
    11.       private void Update () {
    12.         HandleMovement();
    13.         HandelInteractions();
    14.       }
    15.      
    16.        public bool IsWalking() {
    17.          return isWalking;
    18.         }
    19.        private void HandelInteractions() {
    20.          Vector2 inputVector=gameInput.GetMovementVectorNormalized();
    21.  
    22.           Vector3 moveDir= new Vector3(inputVector.x, 0f, inputVector.y);
    23.           float interactDistance =2f;
    24.          
    25.            if (Phyhiscs.Raycast(transform.position,moveDir, out RaycastHit raycastHit, interactDistance);) {
    26.             Debug.log(raycastHit.transform);
    27.            } else {
    28.             Debug.Log("-");
    29.            }
    30.        }    
    31.                        
    32.  
    33.     private void HandleMovement {  
    34.          Vector2 inputVector=gameInput.GetMovementVectorNormalized();
    35.  
    36.           Vector3 moveDir= new Vector3(inputVector.x, 0f, inputVector.y);
    37.            float moveDistance = moveSpeed * Time.deltaTime;
    38.            float playerRadius=.7f;  
    39.            float playerHeight =2f;
    40.            bool canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up *playerHeight, playerRadius,moveDir,moveDistance );
    41.            
    42.             if(!canMove){
    43.               Vector3 moveDirX =new Vector3 ( moveDir.x,0,0).normalized ;
    44.               canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up *playerHeight, playerRadius,moveDirX,moveDistance );  
    45.               if(canMove){
    46.  
    47.                 moveDir=moveDirX;
    48.               } else {
    49.                 Vector3 moveDirZ =new Vector3 ( 0,0,moveDir.z).normalized ;
    50.               canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up *playerHeight, playerRadius,moveDirZ,moveDistance );
    51.               if(canMove){
    52.                 moveDir=moveDirZ;
    53.               } else {
    54.                 // Cannot move in any direction
    55.               }
    56.           }
    57.        }
    58.            if(canMove) {
    59.                transform.position +=moveDir*moveDistance;
    60.            }
    61.  
    62.          isWalking= moveDir!= Vector3.zero;
    63.  
    64.  
    65.         float rotateSpeed=10f;
    66.         transform.forward = Vector3.Slerp(transform.forward,moveDir,Time.deltaTime*rotateSpeed);
    67.         }        
    68. }
    69.  
     
  2. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    This error generally means you have an additional }. And that's one of the reasons I dislike having curly braces on the same line (JavaScript style), as it makes it hard to find this kind of errors.

    Your stray } is probably on line 57, remove it.
     
  3. Wolf_Devp

    Wolf_Devp

    Joined:
    Jan 14, 2024
    Posts:
    7
    Thank you very much for the info..i removed it but it does not work..all { are at their place and still don't work..
     
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    What does "don't work" mean? still the same error? a new error? a runtime exception? no error but the script is not behaving like you want to?
     
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
    its also polite to give the full error which will tell you exactly which line
    if you format your code you will also get a better view of what is wrong
     
    Nad_B likes this.
  6. Wolf_Devp

    Wolf_Devp

    Joined:
    Jan 14, 2024
    Posts:
    7
    Well..it's the same error.. CS1022...
    the script behaves normally but I can't run the game because of the error in code.
     
  7. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    Line 25:
    if (Phyhiscs.Raycast(transform.position,moveDir, out RaycastHit raycastHit, interactDistance);) {


    Remove the ;
     
  8. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    Also Physics not Phyhiscs.
     
    bugfinders likes this.
  9. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    This all should be caught by your IDE (along side fixing the awful formatting). If you're not using one (or using VS Code without proper setup: C# and Unity extensions), you are being mean to yourself.
     
    bugfinders likes this.
  10. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
    But the full error contains the character and line numbers ... :p so it would help everyone to know which line.. which is why the full error is the polite thing to do

    especially as pasting that code in results in 41 errors, the first of which is "error CS1026: ) expected"

    always fix the first error first..

    and as the error is " Assets\<some filename>.cs(25,105): error CS1026: ) expected"
    it tells you line 25 character 105.. so the exact spot where to start looking
     
  11. Wolf_Devp

    Wolf_Devp

    Joined:
    Jan 14, 2024
    Posts:
    7
    It keeps telling me line line 68 character 9...
     
  12. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
    then your code isnt what you posted here.. cos i pasted it in and thats what i got, and as others pointed out there are other errors

    you sure you're looking at the first error? not the last? cos a lot of errors dont mean anything till you fixed those which went before it
     
    Nad_B likes this.
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,710

    You don't need to post for your own typing mistakes. You can fix your own typing mistakes. 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? Are you structuring the syntax correctly? Look for examples!

    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.



    As it seems clear you are not the author of this code, keep these two steps in mind when using other people's code:

    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! See the top of this post.
     
    Nad_B likes this.