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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

A namespace can only contain types and namespace declarations

Discussion in 'Scripting' started by halojman, Oct 4, 2015.

  1. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    Hey I'm trying to create a chat system with an NPC (Non playable character), but Like with my other scripts run into problems I am not qualified to solve (Not enough experience). I followed a tutorial but I get an error message saying:
    'Assets/Script/NPC.cs(1,6): error CS0116: A namespace can only contain types and namespace declarations'

    'Assets/Script/NPC.cs(3,6): error CS0116: A namespace can only contain types and namespace declarations'

    and

    'Assets/Script/NPC.cs(7,6): error CS0116: A namespace can only contain types and namespace declarations'

    And my Script (C#) is :


    bool openChat = false;

    void OnMouseClick()
    {
    openChat = true;
    }
    void OnGUI()
    {
    if (openChat == true)
    {
    //GUI Code here.
    }
    }


    Any solutions?
     
  2. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    wheres your class declaration?

    Code (csharp):
    1.  
    2. public class TestClass: MonoBehaviour
    3. {
    4.       Code goes here....
    5. }
    6.  
     
    AndrewGrayGames and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This. We like typing out extra words in C#. If you want a less verbose language try UnityScript. Trouble is the documentation for UnityScript doesn't have many words in it either.
     
    AndrewGrayGames and KyleOlsen like this.
  4. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    just wanted to ask if class deceleration is hidden in js how to people use features like class attributes inheretence declaring non classed like structs (globally) and blabla. I feel great that I left js before getting serious :)
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    It's optionally hidden. You can declare a class in a .js, you just don't have to.
     
    Kiwasi and Yash987654321 like this.
  6. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27

    Thanks, I'll try it.
     
  7. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    Okay, So I did this:

    public class TestClass MonoBehaviour
    {
    bool openChat = false;

    void OnMouseClick()
    {
    openChat = true;
    }
    void OnGUI()
    {
    if (openChat == true)
    {
    //GUI Code here.
    }
    }

    (Also Let me know if I did something wrong.)
    So I did the method you provided, and I got the following error:

    Assets/Script/NPC.cs(2,36): error CS8025: Parsing error
     
  8. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    put a Colan : between TestClass and monoBheaviour. this is called inheritance. We take MonoBehaviour the base class of TestClass
     
    Magnumstar and halojman like this.
  9. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    Okay I was able to fix my last error! but, I'm Still having a problem with my script, I was able to get the code right by doing this:


    public class TestClass : MonoBehaviour
    {
    bool openChat = false;

    void OnMouseClick()
    {
    openChat = true;
    }
    void OnGUI()
    {
    if (openChat == true)
    {
    //GUI Code here.
    }
    }
    }

    But I got the following error:

    Assets/Script/NPC.cs(1,26): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing a using directive or an assembly reference?
     
  10. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    try replacing MonoBehaviour by UnityEngine.MonoBehavior
    or put
    'using UnityEngine;' at the top
     
    Magnumstar and halojman like this.
  11. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    halojman likes this.
  12. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    Yes, Thanks!
     
    Magnumstar and Yash987654321 like this.
  13. halojman

    halojman

    Joined:
    Sep 28, 2015
    Posts:
    27
    But now how do I get my NPC to work? The script is fine, but I don't know how to get my NPC to chat.
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    At this point, you should start a new post; you now have a very different question, and all these posts just clutter up the thread at this point.
     
    halojman likes this.