A namespace can only contain types and namespace declarations

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?

wheres your class declaration?

public class TestClass: MonoBehaviour
{
      Code goes here....
}
2 Likes

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.

2 Likes

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 :slight_smile:

It’s optionally hidden. You can declare a class in a .js, you just don’t have to.

2 Likes

Thanks, I’ll try it.

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

put a Colan : between TestClass and monoBheaviour. this is called inheritance. We take MonoBehaviour the base class of TestClass

2 Likes

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?

try replacing MonoBehaviour by UnityEngine.MonoBehavior
or put
‘using UnityEngine;’ at the top

2 Likes

@halojman did it worked?

1 Like

Yes, Thanks!

2 Likes

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.

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.

1 Like