Error CS0116 and <invalid-global-code> fix

Hi again!

I am seeing 2 things I am concerned about in my code but am unsure of how to fix them. They are error CS0116 and . Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Linq;
using ScissorsPaperRock;

namespace ScissorsPaperRock
{
    public static void Main() // This is where error CS0116 and <invalid-global-code> are coming from.
    {
        if (thePlayerChosenOption = UNIT.SCISSORS)
        {
            if (theAIChosenOption = UNIT.SCISSORS)
            {
                Debug.Log("The game is a tie!");
            }
            else if (theAIChosenOption = UNIT.PAPER)
            {
                Debug.Log("You win!");
            }
            else (theAIChosenOption = UNIT.ROCK);
            {
                Debug.Log("You lose!");
            }
        }
        else if (thePlayerChosenOption = UNIT.PAPER)
        {
            if (theAIChosenOption = UNIT.SCISSORS)
            {
                Debug.Log("You lose!");
            }
            else if (theAIChosenOption = UNIT.PAPER)
            {
                Debug.Log("The game is a tie!");
            }
            else (theAIChosenOption = UNIT.ROCK);
            {
                Debug.Log("You win!");
            }
        }
        else (thePlayerChosenOption = UNIT.ROCK);
        {
            if (theAIChosenOption = UNIT.SCISSORS)
            {
                Debug.Log("You win!");
            }
            else if (theAIChosenOption = UNIT.PAPER)
            {
                Debug.Log("You lose!");
            }
            else (theAIChosenOption = UNIT.ROCK);
            {
                Debug.Log("The game is a tie!");
            }
        }
    }
}

I did some reading on here and it seemed like the fix was to create a public class and put it in there, but all that did was flood my console with even more errors so I reversed this change. How can I fix this without just flooding my code with even more errors?

Next is the which occurs when I hover over Main() I frankly have no idea what it means, if it will affect my game, and how to remove it. This post seemed to imply that it was a bracket, but not much helpful came from it. I made sure to scrub through my code for any loose brackets and couldn’t find any. When I click on it though, it highlights the entirety of my code, which is everything I posted. Should I worry about it, and if I should, how can I fix it?

Thank you in advance!

My best guess is Main is a special method, so I would change the name of the method. Main is something I’ve seen back when I was doing windows form apps but I also think Unity treats it as a special method.

Try a different name for it.

The error doesn’t mention “special methods”, just methods in general. It says:
error CS0116: A namespace cannot directly contain members such as fields or methods

I don’t feel like changing Main to something else (even if I knew what else I could change it to) will quite fix the problem. But if you’ve got a suggestion for what other names I could use, please let me know what!

Thanks.

Oh, I think you meant a different name in the namespace. My bad. I tried changing the name though and it didn’t fix the error!

Oh, duh. My brain was broken. You don’t have a class. Main is a method inside a namespace, but you’re missing a class.

  • using System.Collections;

  • using System.Collections.Generic;

  • using UnityEngine;

  • using UnityEngine.EventSystems;

  • using System.Linq;

  • using ScissorsPaperRock;

    • namespace ScissorsPaperRock
  • {

  • public class MyClass //Should match the script name

  • {

  • public static void Main() // This is where error CS0116 and are coming from.

  • {

  • //code

  • }

  • }

  • }

You also don’t use a Main method when creating stuff for Unity. Main is the entrypoint of your program, but Unity already defines it.

If you want to follow a generic C# tutorial (you should!), don’t run it in Unity, just write it as a plain C# app in something like Visual Studio Code and run it from there.

As I said in my post, I tried creating a public class and doing it caused way more errors. Is there any other option than this? If this is the only option, I’ll use it and deal with the bugs that follow, but if there’s another option that (probably) won’t flood me with its own errors, I think that would be better. Thank you for your help though!

I wasn’t following a tutorial for this…

Then maybe you should! Your code doesn’t make very much sense, and the “what do I need to write to make the errors go away” approach isn’t going to get you far.

I think the confusion is what are you trying to do. From a glance it looks like a paper rock scissors game, but not for how I’d expect to see a script written for Unity would be.

@Baste and I seem to be in a similar mindset with your use of Main seeming typical of a more standard c# app.

But, from looking at your code, the reason you probably get errors is you have if statements that seem to reference what I assume should be variables, but the variables aren’t declared anywhere nor are they parameters being passed into the method “Main”. UNIT also appears to be an enum, but has that been declared anywhere?

Since you mentioned you weren’t following a tutorial, I’m curious as to if you’ve had some coding experience before and were leaning on that knowledge or how the creation of this code came about.

Either way, if you aren’t familiar with coding for Unity, it may not hurt to look at a few tutorials. I know when I first started with Unity I had a programming background that included c#, but not within Unity and I had to learn about components and Unity’s magic methods and many more things that were Unity focused.

The code you posted has a lot of problems. More than two errors. Just eyeballing it quickly I’m counting no less than 28 things that would cause a compiler error (that’s not exaggerating, I literally counted that many). The only reason you’re only seeing two errors is because those errors are so dominating that the compiler doesn’t even know how to make enough sense of your code to tell you what’s wrong with it.

So when you have put it into a class, and fixed that problem, the compiler is just going: “Oh, NOW I understand what this is supposed to be! And here are all the things wrong with it.” So what you’re seeing as “more errors” is, in fact, closer to being correct.

(In addition to the issues @Brathnann mentioned of using undeclared enum types and undeclared variables, you’re also using assignment operators in a conditional statement, and you’re also putting conditional statements after “else” which is not allowed.)

And even if you do go through and fix all those errors, you’ll still have the problem everyone’s telling about where “Unity doesn’t work that way”, and the compiler won’t tell you about this. Because it is valid code to have a function called Main, but unless you’re writing it as a natively C# program (and not a Unity one), there’s no reason to expect that function to do anything, unless something else is going to be calling it. That’s what we mean when we say Main() is special.

Given the fact that my code clearly doesn’t make sense in Unity’s context, maybe scraping it clean and restarting is the best option. Especially given everyone else saying “maybe a tutorial is the right idea!”

This is my first time coding in Unity, and I was basing it off something that was coded solely in Visual Studio. So I applied my own “knowledge” and figured that what worked in solely VS would work here. I noticed discrepancies early on but passed them off assuming they were working but weren’t showing me, while secretly dreading the idea that they weren’t working.

Said variables and enums are declared on other files. I noticed very, very early on that when referencing variables and enums from other files, that small drop-down list above that showed all the references wasn’t showing up. I absolutely dreaded reading this because it confirms that no, not a single one of my enums are being referenced outside of their file. Ouch.

I’m a student, so I’ve been basing this coding off what I “learned” from said tutorials. But this is my first time using Unity, as all previous projects were created and designed solely in Visual Studio as native C# programs. It is clear to me, that what works in Visual Studio alone won’t work in Unity.

Thank you very much for your help. I can see now that the problems are more fundamental than “This has errors”. I think I’ve got some tutorials to search up…

It has become clear to me that yes, Unity does not work the way I think it does. I’ve got a lot of work ahead, and probably a few tutorials. Thank you for your help!

No problem. About 6 years ago I was where you seem to be now. I knew c# and after finding out Unity used c#, thought it would be a perfect match up. While my c# knowledge did help, some of the Unity unique stuff did throw me off a bit at first, but it becomes pretty second nature and I think since you have programming knowledge, you’ll pick it up pretty quick.

Good luck!