Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question im getting the error Top-level statements must precede namespace and type declarations. hers my code

Discussion in 'Getting Started' started by Thecatking12, Sep 15, 2023.

  1. Thecatking12

    Thecatking12

    Joined:
    May 27, 2023
    Posts:
    7
    using UnityEngine;

    public class WeaponSwap : MonoBehaviour{}

    public int selectedWeapon =0;
    {
    // Start is called before the first frame update
    void Start(){
    SelectWeapon();
    }

    // Update is called once per frame
    void Update(){

    }
    void SelectWeapon ()
    {
    int i = 0;
    foreach (Transform weapon in transform)
    {
    if (i == selectedWeapon)
    weapon.gameObject.SetActive(true);
    else
    weapon.gameObject.SetActive(false);
    i++;
    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,278
    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?

    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.

    In this case you have open bracket / close bracket at the end of your class declaration line, making ALL your code actually be outside of a class.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,840
    Your first error is that you failed to use code tags here on the forum. The top sticky post in the scripting forum explains how to use code tags. Your second error is that you seem to don't understand one of the most fundamental C# language elements: curly braces. Do you understand why they exist and what they are doing? What do you think this line does?

    Code (CSharp):
    1. public class WeaponSwap : MonoBehaviour{}
    Do you note the opening and closing braces at the end? Does that make any sense to you? You define a class that is derived from MonoBehaviour, but it's body is empty. You know the body of a class is supposed to be inside those curly braces :)
     
  4. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,195
    @Thecatking12 , you would do yourself a great service by taking a few small C# tutorials on the web. It's an awesome language, and a few hours of instruction will pay you absolutely massive dividends in time saved in the future.
     
    Chubzdoomer, Kurt-Dekker and Bunny83 like this.