Unity C# Class and Objects

unity class definition
unity class constructor
unity monobehaviour
unity classes near me
social class unity
unity custom object
unity list of classes
unity inheritance

Alight, I apologize that this will be a rather dumb question, but I am trying to gain a full understanding of the purpose and use of objects. Specifically I am working in Unity, but I am not sure that this matters. Also, I should also mention that the language is rather overwhelming. But here is the simple example that I am working on.

public class Player : MonoBehaviour 
{
    private string name = "Players Name";

    private void Start () {
        var nexus = new Player();
    }

    public static void Using() {
        // how are these different?
        Attack();
        this.Attack();
    }

    public static void Move() {
        print ("The player is moving"); 
    }

    private void Attack () {
        print ("The player is attacking"); 
    }
}

public class UsingStuff : MonoBehaviour 
{
    private void Start () {
        Player.Move();
    }
}

So here are the questions:

  1. What is the difference between calling a function Attack() versus this.Attack? From a novice perspective, they would seem to do exactly the same thing. However, I am assuming that my example is just too simplistic.
  2. I created the object of the Player class with the random name nexus. However, when I call a function from a different class I seem to use the class name rather than this variable name. So what is the purpose of creating a variable name?

I will leave it at those two for now, as hopefully that will help with some confusion for some other things. Thank you for your help.

What is the difference between calling a function Attack() versus this.Attack?

When you are coding the class Player for example and you want to use the method "Attack()" you can do that by typing Attack() or this.Attack(). It does the same thing in both cases.

When you use "this." you guarantee that the method you are using is the implemented on the class you are on. For example:

public void GroupAttack(DifferentPlayer ally){
    Attack(); 
    ally.Attack();
}

public void GroupAttackWithThis(DifferentPlayer ally){
    this.Attack(); 
    ally.Attack();
}

There is no difference between the method GroupAttack and GroupAttackWithThis.

Useful link to see the difference: In Java, what is the difference between this.method() and method()?

I created the object of the Player class with the random name nexus. However, when I call a function from a different class I seem to use the class name rather than this variable name. So what is the purpose of creating a variable name?

It happens because you made the function "Move" static, it means that for every instance of that class it will do exactly the same. Static is used when you want a method or a variable to have the same value or have a behavior identical every time for every instance of the class. If your method is not static you will need to use the name of the variable to use it, that is because you will want your method to change his behavior for every instance. A simple example:

public string GetName(){
    return name;
}

This method will return for every Player a different name (will return the name of the player in the variable). So if you do:

Player jhon = new Player();
jhon.name = "Jhon";
String jhonsName = jhon.GetName();

The variable "jhonsName" will contain a string with the value "Jhon".

I hope this could be useful for you and good luck with your game.

Classes, How to use Classes to store and organize your information, and how to create an Instance (an Duration: 6:33 Posted: Apr 18, 2019 Unity C# Class and Objects. Alight, I apologize that this will be a rather dumb question, but I am trying to gain a full understanding of the purpose and use of objects. Specifically I am working in Unity, but I am not sure that this matters. Also, I should also mention that the language is rather overwhelming.

When you execute Player nexus = new Player();, you are creating a new instance (/object) of type (/class) "Player". The variable nexus is an object reference (i.e., it refers to the object instance you just created). Once you have an instance, you can call instance methods on that object, set or read its properties (which, by the way, you have none of). Since there is only one reference to that object (the nexus variable), when that reference goes out of scope (at the end of the function), the object no longer is referenced by anything and is eligible for garbage collection.

When you declare a method (/function) with the static keyword, you are creating it at the class level, rather than at the instance level. If you wanted to call the non-static Update or Attack methods, you'd have to say nexus.Update or nexus.Attack. To call the static Using or Move methods, you'd use the class name (Player) like this: Player.Using or Player.Move.

The code you posted does not compile, does it? You can't call an instance method from a static method. You can't call the instance method Attack or this.Attack from the static method Using (there's no "this" there (this is the current object instance associated with an instance method)). It's best to post code that at least compiles (unless you say (in a comment) //this is the code that doesn't compile

You also only create a new Player instance in the Start method, which is a little odd. Generally, you create instances of objects in other classes or in static factory methods (not always, but generally).

Read up on the nature of object oriented programming, on C#'s static keyword and on the nature of object instances (in general). You probably want to read up on C#'s properties as well.

Oh, and naming something Using is going to hurt in the long run (lower case using is a C# keyword that already has two meanings).

Finally, to answer your question, "calling an instance method this way: Attack() or that way: this.Attack() has the same semantics." Of course, as noted above, you can't use either form if you are trying to call an instance method from a static method.

7 - How To Program In C# For Unity - Classes And Objects, If you are a complete beginner and want to learn how to make games click on the link below and Duration: 8:15 Posted: Sep 4, 2015 The language that’s used in Unity is called C# (pronounced C-sharp). All the languages that Unity operates with are object-oriented scripting languages. Like any language, scripting languages have syntax, or parts of speech, and the primary parts are called variables, functions, and classes.

There are some differences between Unitys MonoBehaviour to the normal C# classes. I recommend to watch some Unity Tutorials. Unity-Scripts are Scene oriented.

  1. There is no difference. this.Something can be used to mark something as "from the class object" (Normally Properties or Variables) to distinguish from parameters.

  2. Writing Player nexus = new Player(); should be a Warning because you should not do this with a MonoBehaviour.

You create a Player in the Scene by putting the Script "Player" on a GameObject. You should access the Player script from the GameObject. So you should remove "static" on the methods. Then put public Player player; inside UsingStuff (under public class UsingStuff : MonoBehaviour {). Then Drag your Player on the placeholder for Player. You will find it on the GameObject with the UsingStuff-Script in the scene. Now you can call player.Move() (with small p) in the Start Method.

Try another naming for your variable name. It already exist for every MonoBehaviour. It refers to the name of the GameObject the Script is lying on. Your naming should also throw a Warning.

Notes for Noobs, Classes are blueprints for your objects. Basically, all of your scripts will begin with a class declaration that contains something like this: This tells Unity you're  And that actually is an object. It's an instance of the print message class that gets attached to the game object. To recap, the scripts we write in Unity are actually classes. And once we attach a script to a game object in a scene, then that game object gets an instant of the script class, so it gets an object attached to it.

  1. The this keyword means "A reference to the current instance of the class" It's syntactic sugar really (it can make your code clearer to read)

When you do

public static void Using() {
    Attack();
    this.Attack();
}

Behind the scenes, the compiler will execute it as so:

public static void Using() {
    this.Attack();
    this.Attack();
}

so in your context, they are the same.

There are times it is useful see this post

  1. If I understand correctly, this question relates to why do you need to have a variable called name rather than just naming the object as the player name. The variable "name" and the name of the object are two different things

Lets say you want to output the players name to the screen

void Start () {
    Player nexus = new Player();

}

in the method above, i can't [simply] do it

void Start () {
    Player p = new Player();
    p.name = "nexus";
    Console.WriteLine(p);
}

Whereas here I output the value in the variable called name. Variables are basically a box with name on (name) which hold values (nexus)

I suggest you look at some basic programming tutorials before you tackle Unity

Unity C# Class and Objects, What is the difference between calling a function Attack() versus this.Attack? When you are coding the class Player for example and you want to  How to Use a Class in Unity. In this post, I will be using C# since it is my preferred language in Unity. Each language that implements classes has its own syntax for declaring and using classes. In C#, you would create a class like this: class Vehicle { int tires, year, hp, topSpeed; string model; void Accelerate()

On top of the other answers, you can also use this to disambiguate. Common example:

public MyType(this name) => this.name = name;

Or to use Extension Methods:

public static void MyCustomExtensionMethod(this Foo foo) { }
...
public class Bar() : Foo
{
   // I did not create Foo and do not have access to Foo
   this.MyCustomExtensionMethod();
}

Scripting API: Object, Versions without this page: Supported. Legacy. LanguageEnglish. English. C#. Scripting API. Version: 2019.3. Select a different version, 2020.1 - Supported  Classes and objects. Classes are the most fundamental of C#’s types. A class is a data structure that combines state (fields) and actions (methods and other function members) in a single unit. A class provides a definition for dynamically created instances of the class, also known as objects.

Learning C# and coding in Unity for beginners, are not limited to just variables. A vehicle also has the ability to accelerate, for instance. It is used to initialize objects of that class type. Add the following constructor to the BankAccount type: public BankAccount(string name, decimal initialBalance) { this.Owner = name; this.Balance = initialBalance; }

How to create custom classes/objects in Unity? : Unity3D, about C# and the basics of coding, like variables, functions and classes, and how to All the languages that Unity operates with are object-oriented scripting  In c#, Classes and Objects are interrelated. The class in c# is nothing but a collection of various data members (fields, properties, etc.) and member functions. The object in c# is an instance of a class to access the defined properties and methods.

Introduction to C# Programming and Unity, How to create custom classes/objects in Unity? Like if I wanted to make the classic "CoOrds" class: class CoOrds { public int  C# is based on the C++ programming language. Hence, the C# programming language has in-built support for classes and objects. A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity. For example, if you want to work with employee's data in a particular application.

Comments
  • 1) There is no difference in this case. 2) You need a fundamental understanding of variables, properties, and scope. You should have a Player class member that you instantiate in Start(). Instead, you're creating a player and then throwing it away because it goes out of scope when Start() returns.
  • For #2, look at C# static vs instance methods to learn why sometimes you need an instance of a class (the variable name) to call a method, and other times you just use the class name (for static methods).
  • You should have the Update function it it's not in use, it's worse on preformance. Also, you shouldn't include it in the Stack Overflow post as it's not relavent to your issue.
  • I suggest you take a good amount of time to study C# and core programming concepts by themself before trying to make a game. It took me quite a while before I finally was ready for a game. Right now, you're learning to very complex things at once and that's hindering your learning of both things.
  • Note that the code in the Using function will not compile because it's a static function while Attack is a non static function. You can't call non static function from a static function.
  • And, that first sample of code (calling the instance method Attack from the static method Using) won't compile. The two Start method examples are a little weird too. As @itsme86 noted, the first one creates a new Player object, but, since the reference immediately goes out of scope, that object is lost forever and is eligible for "Garbage collection". The second example would only work if the Player class had a public string property named name (which, by convention, should really start with a capital letter). The WriteLine call relies on Player having a ToString method override
  • NB, Just quoting the OPs code for the purpose of explanation. Have left out comments re: code quality of and other factors which are outside of the scope of the question
  • This site is not like forums. You should not post replies as answers. You can use comments, or open a new question.
  • Agreed, probably best to add and edit to the bottom of your original post because this will get missed as it is classed as an answer