Search Unity

Discussion HELP! CS0116!

Discussion in 'Scripting' started by NoSleepKid, Feb 14, 2023.

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

    NoSleepKid

    Joined:
    Aug 23, 2022
    Posts:
    4
    So, I Have A Error That Says This:
    Assets\Scripts\UI Items.cs(3,12): error CS0116: A namespace cannot directly contain members such as fields or methods. PLEASE HELP!


    Code (CSharp):
    1. function Start();
    2.  
    3.     public GameObject;
    4. {
    5.     (input.GetKeyPressed(KeyCode.E));
    6.     GameObject.SetActive(true);
    7. }
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Use update instead of start so it's always looking to see if the key was pressed. The GameObject needs to be defined outside of the function. In C# functions are declared as "void" rather than the javascript style of "function."

    Code (CSharp):
    1. public GameObject myObject;
    2.  
    3. void Update () {
    4.  
    5.         if (input.GetKeyDown (KeyCode.E)) {
    6.             myObject.SetActive (true);
    7.         }
    8.  
    9. }
     
    NoSleepKid likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What is this? This can't be your entire script. It looks like UnityScript because of function Start();, but unless you're on an old version of Unity, that isn't supported. I'm not sure what you've actually wrote. I would suggest looking at a proper c# tutorial.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,866
    Your syntax is still completely wrong and not at all how C# is written.

    You need to do some C# tutorials.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    To clarify, void just defines the return type as nothing. Since you could have a return type, void wouldn't be part of the method signature in those cases.
     
    QuinnWinters likes this.
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Your problem is several typos and incorrectly formatted code. Please review tutorials and beginning coding courses. Do not in the future just slap some code in a post and say "help!" that is not what this forum is for. Closed.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.