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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Type or namespace definition, or end-of-file expected

Discussion in 'Scripting' started by Epic7Ate9, May 23, 2020.

Thread Status:
Not open for further replies.
  1. Epic7Ate9

    Epic7Ate9

    Joined:
    Apr 30, 2020
    Posts:
    7
    Im new to unity and C# im also following a brackeys tutorial on how to do controller inputs, the code itself im assuming is fine, but im getting this stupid error, does anyone know what it means and how to fix it?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class P2Script : MonoBehaviour{}
    7. public Rigidbody rb2;
    8.  
    9. {
    10.  
    11.     P2Control controls;
    12.  
    13.     void Awake()
    14.     {
    15.     controls = new P2Control();
    16.  
    17.     controls.Gameplay.Left.performed += ctx => Left();
    18.     }
    19.  
    20.     void Left()
    21.     {
    22.        rb2.AddForce(-3, 0, 0);
    23.     }
    24.  
    25.     void OnEnable()
    26.     {
    27.        controls.Gameplay.Enable();
    28.     }
    29.  
    30.     void OnDisable()
    31.     {
    32.        controls.Gameplay.Disable();
    33.     }
    34.  
    35.  
    36. }
    Thanks.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Your class starts and ends in this line, due to opening and closing parentheses:
    Code (csharp):
    1. public class P2Script : MonoBehaviour{}
    Thus the other parts of your script do not belong to any type, which is invalid.

    It seems you already have the actual parentheses for the class in place (opening in line 9, closing in line 36).
    Remove the parentheses in line 6 and move line 7 into the type's scope, e.g. line 10.
     
    Bunny83 and leftshoe18 like this.
  3. Epic7Ate9

    Epic7Ate9

    Joined:
    Apr 30, 2020
    Posts:
    7
    Yea but now im having some issue where if it forces me to add arrow parentheses otherwise it wont let me play my game.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    @Suddoha pointed out fundamental syntax construct errors in your script. If you believe you fixed them, repost the updated script with code tags.

    Help us to help you.
     
  5. irfanis

    irfanis

    Joined:
    May 29, 2022
    Posts:
    1
    i am getting the same problem and i am new to unity
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Please don't necro-post to old threads for your own typos. Instead, just go fix your own typos. Here's how:

    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.

    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.
     
Thread Status:
Not open for further replies.