3

A little background: I'm new to C# and Unity, but catching on very quickly. I'm also hoping this thread will not spark a debate about the merits of classes and abstract coding, as that debate is unrelated and well-worn (and unnecessarily heated); so please keep that in mind.

I'm simply wondering if every C# script in Unity is required to have a main class in any way or for any reason.

Or instead, can methods, and variables can be written outside of a class in a blank file (with namespaces) to be used in a video game?

I'm asking because, when I create a new C# script, it seems to force a class into my file and I'm afraid of breaking things.

  • I hope to keep code abstraction to a minimum, and the current project I'm working on has several situations where a class is not needed, or only one instance of the class will be used. I'd like to simply avoid using classes in those cases.
John Lardinois
  • 109
  • 2
  • 11
  • 1
    hi John, the simple answer is "yes, of course". When you say *"I'd like to simply avoid using classes in those cases."*, just forget about that, it's completely meaningless. Say you have some extremely simple task to achieve (what about "blink on and off"). OK, so it's one line of code, in a simple class called "Blink" in a file "Blink.cs". That's all there is to it. Drop the file on the objects which need that "blink" behaviour. – Fattie Apr 11 '16 at 11:15
  • Sorry, Joe Blow, all of these answers were very helpful and I struggled to pick the right one. I learned a lot and wish I could have picked all three answers. – John Lardinois Apr 13 '16 at 01:44
  • 1
    well done, it's important for new users to Tick an answer! you get points. Cheers. – Fattie Apr 13 '16 at 01:56

3 Answers3

2

In terms of declaring/defining variables and methods outside of any class, you can't really do that in C#. It just isn't how the language was designed (the answers to the question I linked to expand on that idea, so I won't duplicate them here).

You're not without options, though; if you have a number of variables or methods that need to be accessible from different places and don't need an object reference, you can make them static, so you won't need to instantiate the class to make use of them:

public class UtilityClass
{
    public static float GravityConstant = 3.51f;
    public static string GameName = "MyFirstGame";

    public static float CalculateProduct(float a, float b)
    {
        return a * b;
    }
}

Then, you can reference the class's methods/members by accessing it through its name:

float product = UtilityClass.CalculateProduct(6, 1.5f);

An example of where you might use this pattern is when defining mathematical formulae which aren't included in Unity's Mathf methods, and using them in multiple classes.

Additional note: Creating a new C# script through Unity's editor UI will default to declaring a class of the same name that inherits from Monobehaviour. You can alter it to remove the inheritance from Monobehaviour if you don't need any of the methods/attributes of the class, which avoids unnecessary overhead. One example for this would be with a static class that you never need to instantiate.

Community
  • 1
  • 1
Serlite
  • 12,130
  • 5
  • 38
  • 49
1
  1. First of All you need to check where Methods define as offical docs stated

    "Methods are declared in a class or struct by specifying the access level such as public or private...."

    So, Method should be declare in a Class or struct and A given class should be, ideally, responsible for just one task.(see also)

  2. Your this question "Or instead, can methods, and variables can be written outside of a class in a blank file (with namespaces) to be used in a video game?" answer is hidden in the below question.

    Can there be stand alone functions in C# without a Class? No. Make them static and put them in a static utility class if they indeed don't fit within any of your existing classes.

    You have to make a class in order to use methods or its variable either instance class or static class.

  3. Am I required to use Classes for every script? Every script means you required a class. Unity Support Component Based Architectural Design and if you require any script related work then you definitely require a script component which means a class require.

  4. Finally for singleton, thanks to Unity3dWiki great detail available. I think you will be feel comfortable to code and writing class if you keep in mind component based architecture of Unity3d.

  5. Singleton vs Static: I will also recommend to check this: Why do you use a Singleton class if a Static class serves the purpose

Hope it will help.

[Note: If this helpful Any one can update this answer for future reference and use].

Community
  • 1
  • 1
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
1

Yes, you are.

In C#, things like global variables and functions just do not exist. Everything must be contained in a class.

"But what should I do in order to declare some stuff that can be accessed everywhere, without creating an object?" you asked. There is something called the static modifier. You can access the methods or variables or fields or properties marked with this modifier without creating an object of that class.

You just add the word static in a method and it becomes a static method! How simple!

Let's see an example.

I have this non-static method:

public class MyClass {
    public void DoStuff () {

    }
}

I can call it like this:

var obj = new MyClass();
obj.DoStuff();

But if I modify it with static,

public class MyClass {
    public static void DoStuff () {

    }
}

I can call it like this:

MyClass.DoStuff();

How convenient!

Note:

Please do not misuse the static modifier! Only use it when it makes sense! When? When the method is a utility method or when the method does not belong to individual objects but the class itself.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    Are you trying to sell something? Please do not use these rhetorical "how convenient / how simple" questions, they don't belong into a answer. Also, global vars actually do exists, but only declared inside a class. – Jannik Apr 11 '16 at 05:13