Assets/TextController.cs(30,14): error CS0116: A namespace can only contain types and namespace declarations

So I am currently in the making of my text adventure game and I am confused why this error has come up, I followed the previous two examples led in the lecture and yet, when I tried my third “zone” the state_stealth is read as seen in picture below.

I have made sure that myState is set and there seems to be no errors there, I am confused if I have just done a wording error or I missed a bit of code? here is what the paragraph of the code looks like

void state_stealth() {
text.text = "You slowly creep down the corridor your snake like and pyro friend " +
"are closely following gently whispering the horrid images that they " +
“can see… Aren’t you glad you can’t?\n\n” +
“Press D to continue into the darkness or R to return to the stairway”;
if (Input.GetKeyDown(KeyCode.D)) {
myState = States.darkness ;
if (Input.GetKeyDown(Keycode.R)) {
myState = States.stairwell ;

Many thanks to anyone who is able to help I will keep checking it but it’s currently 22:10 and not slept in over 24 hours so gonna attempt sleep no hope everyone here is having fun with theirs :smiley:

just wondering have you got a function called state_stealth() with that spelling and case?

Hey OboShape thank you for getting back to me, I have just checked and for some reason it is now saying that state_stealth() is ok but now saying my state_stairway() and state_call() are now doing it I am gonna quickly rewrite their functions fingers crossed it works if not will try and re do the lecture and the coding from scratch again.

Ok I fixed the issue by removing the } right next to the edge however it is now saying my keyword void cannot be used in this context?

are you closing the bracket beneath it?

1 Like

There is a closing } missing from that first if statement in your screenshot.

1 Like

Thank you Rob just sorted that now, this is the first time i’ve ever seen or tried coding so it’s really confusing to understand :frowning:

1 Like

Dont sweat it, it gets easier :slight_smile:

With the curly braces they are used for code blocks, so for each opening one you need a closing one.

You may find later on using an IDE like Visual Studio more helpful as not only does it tend to put them infor you but it can also flag when they are missing. That said, learning to spot them is all apart of it too :slight_smile:

2 Likes

agreed I am also getting this “error CS1525: Unexpected symbol (', expecting)’, ,',;’, [', or=’” to do with the same line as my void state_call () {

is it missing a symbol or am i missing something behind it?
(very tempted to start from scratch see if I have the same problem)

As per @Joao_Dalvi’s reply, it is also missing a closing } assuming that the screen shot is the whole method… is there any other code for that method not in the screen shot?

There’s no need to start again, as I said these should be in pairs, same numver of opening braces as closing, if you are not getting any error above the method you mentioned then the code above is most likely all ok. All you need to do is check you have the correct braces working your way down. If there is one missing on this method just add it. If there are still problems it will update and point out where. Just a case of working through them. If you have to spend a bit of time on it and fond where they are missing it will most likely serve as a great lesson in its own right - you will always think about the braces when writinf your next method :slight_smile:

1 Like

Here is the entire script :slight_smile: been rapping my brain around it and still can’t fix it

you need to close the } after the first if under state_stairway

Yep - same issue as the other one - I believe in the lecture you copy and paste one of the methods to save typing it all out, this is fine if it’s error free, but if not, you will obviously repeat the same issue throughout the script :slight_smile:

Here’s a screenshot adding to what @Joao_Dalvi has helped you with :slight_smile:

If I didn’t have Visual Studio pointing them out for me, I would often count the brackets/braces. Might sound daft but I’d count the opening brackets/braces first, 1…2…3…4, then deduct from that count for each of the closing brackets/braces, e.g. 4…3…2 - “Oh, I missed one!” etc etc

Over time you tend to just spot them, especially if you lay your code out neatly, it makes things a lot easier to spot.

In the image above I’ve effectively done the same thing, counted and stated open / close - so from that you can see that there wasn’t a “2 Close”. I’ve squeezed one in there to show where @Joao_Dalvi stated.

If you have any other blocks of code for the states that were copied from the original one at the top of your script you may find you have a couple more of these to resolve, that said, from the screenshots you’ve provided, the rest looks good :slight_smile:

1 Like

Adding to what @Rob said, you can also mouseover at any bracket/brace and monodevelop will highlight it’s pair for you (if you rest your pointer at one opening bracket it will show which bracket is closing it), at least for me, it makes easier to see if everything is where it should be.

1 Like

That’s handy to know Johnny, I don’t tend to use MonoDevelop but good to know some of that useful functionality is there. :slight_smile:

…and there’s me counting eh… *tut* old school habbits :wink:

1 Like

Yes, this funcionality is very handy!
But I think that the best way to avoid these problems still is to use a good indenting style and stick with it, it helps reading the code too.

1 Like

I would agree, I often write my methods as just stubs initially, e.g.

public void MyMethod()
{

}

Then write the middle bit afterwards as it’s easier to see at a glance if things are missing. It all comes with time and experience though doesn’t it. I know when I first started out I would have spent a lot of time looking for a character I had missed here or there. Whilst the IDEs get more advanced and helpful there is a tendency to rely on them. Nothing like a day of writing code in Notepad to spice things up a bit :smiley:

1 Like

Lol, I use the same indenting style as you, and I also write the middle part afterwards. Notepad coding is hardcore mode for me :joy:

Oh my, many people here have over a decade of experience with coding and I’m starting into it just now, I have a ton to study and to learn :sweat_smile: it is very good to have experienced people around here sharing their experiences, it helps a lot

hehe, it’s a quick way to see how much you can retain… not only the brackets, but the namespaces too! woah! hehe… I don’t think I could get very far with C# like that at the moment to be honest, my main .Net experience was with VB.Net, thankfully it’s mainly just syntactical changes, in fact, in my head, if I reverse how I did stuff in VB it seems almost correct in C# :smiley:

VB:
Private _numberOfIncorrectLinesOfCode As Int

C#:
private int _numberOfIncorrectLinesOfCode;

That was one of my biggest battles, wondering why all these C# people liked everything back to front! hehe, just depends on your perspective and what you are used to. If you are coming at it without any previous experience, little things like this don’t really matter :slight_smile:

Don’t even get me started on the semi-colon of doom…!

Well I wouldn’t have guessed that Johnny, you have helped numerous people on the forums here, you are always keen to offer help/suggestions and lots of people have benefited from it - well done you :slight_smile:

1 Like

Privacy & Terms