-2

When I code in general c# and create instance methods, to call them from the same class I have to instantiate the class first and then use the method. But when using unity, The instance methods I create can be directly accessed in the same class(without instantiating the class).

Why is that ? Please help :-)

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 2
    See [Static Classes and Static Class Members](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members) – Filburt Jun 14 '21 at 07:59
  • Welcome. Could you provide more details? Maybe the methods you're calling are defined in some of the base classes. – devsmn Jun 14 '21 at 08:00
  • 1
    Does this answer your question? [What's a "static method" in C#?](https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c) – Filburt Jun 14 '21 at 08:00
  • Please add the code of what exactly you are referring to – derHugo Jun 14 '21 at 08:24
  • @Filburt I don't think this is actually about `static` at all. We will need more information from OP but as I understand it the question is about something else – derHugo Jun 14 '21 at 08:39

2 Answers2

1

Besides what everyone says here about static members as I understand your question it has nothing to do with static but you rather wonder why you nowhere are using new for your own or Unity built-in components.

Whenever a component is attached to a GameObject => an instance already exists because Unity already created it for you. Further there are certain properties like e.g. transform and gameObject which Unity already populates as well for you while attaching that component to a GameObject.

And of course an instance of your custom component itself can access all of its instance members.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • okay, so what you are saying is that like i created a custom component and when i am working on it, at the same time i am working on the instance of that component ? – The Noobest Guy Jun 15 '21 at 08:21
  • @TheNoobestGuy what I am saying is: Please add your code and what exactly you are doing/trying to do ;) By `created a custom component` do you mean the script or do you mean you added that component to a GameObject? And by `when i am working on it` again do you mean on the script of that component or what exactly are you referring to? – derHugo Jun 15 '21 at 08:24
  • @TheNoobestGuy I saw you edit .. please do not edit additional information into answers but rather your question. And yes, the things you are accessing there are all **instance** members and will be called/used on a certain **instance** of that component. You didn't show as what exactly `isMovementEnabled` is declared though – derHugo Jun 15 '21 at 11:28
  • isMovementEnabled is responsible for just turning the compnent off and on – The Noobest Guy Jun 17 '21 at 09:24
  • so its correct to say that when i am working on the script and have instance methods, i can call them directly without instantiating the class because in the script i am actually working with some instance itself ? :-) – The Noobest Guy Jun 17 '21 at 09:25
  • Well not like this no .. but yes what you mean is correct: Later you definitely will work with instances of that script, namely the components attached to GameObjects in your scene or prefabs -> so yes you will have instances – derHugo Jun 17 '21 at 09:53
0

Because of static members and classes. With the static keyword, you need not to instantiate the class to access its members. Also, you cannot instantiate static classes.

bg117
  • 354
  • 4
  • 13