1
public class AudioManager : MonoBehaviour 
{
    private static AudioManager audioManager = null;

    private void Start () 
    {
        if (audioManager != null) 
        {
            Destroy(gameObject);
            print(audioManager + "'s self destroy requested.");
        } 
        else 
        {
            audioManager = this;
            GameObject.DontDestroyOnLoad(audioManager); 
        }
    }
}

I know this code's function which is if there is an audioManager than destroy the new and than the audioManager only exist once so the audio of the audiomanager doesnt make a new audiomanager and so there wont be loop so i am going to hear the attached audio file only once. But can someone explain me that what is "this" and what is it used for and do the same with "static" keyword? Thanks for the help, Kristóf

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • 4
    Did you try looking up a tutorial on C# language? Or read the documentation about those keywords? I feel these are much better explained by reading those and not really something to explain here or we will have hundreds of questions of keywords. – Sami Kuhmonen May 19 '18 at 10:01
  • @SamiKuhmonen a bit blunt. I think marking duplicate would be just as effective – flakes May 19 '18 at 10:04
  • 2
    @flakes Instructing where to find information in the future instead of just duping is blunt and bad? They’ll save a lot of time by going to documentation next time. – Sami Kuhmonen May 19 '18 at 10:07
  • i watched documents and tutorials too but they werent too bright for me –  May 19 '18 at 10:16
  • For `this`, possible duplicate: [What does $this mean in PHP?](https://stackoverflow.com/questions/4124982/what-does-this-mean-in-php). For `static`, possible duplicate: [What does “static” mean in C?](https://stackoverflow.com/questions/572547/what-does-static-mean-in-c) – VA systems engineer May 19 '18 at 10:22
  • 1
    Possible duplicate of [What does "static" mean in C?](https://stackoverflow.com/questions/572547/what-does-static-mean-in-c) – VA systems engineer May 19 '18 at 10:25
  • @SamiKuhmonen like this: https://stackoverflow.com/questions/572547/what-does-static-mean-in-c?noredirect=1&lq=1 ? made static by mods. – Confused May 19 '18 at 18:13
  • @NovaSysEng have you read the question and answer you're linking as duplicates? The only thing i see them having in common is the word static. – Confused May 19 '18 at 18:14
  • @Confused: See my two comments: one links to a `this` question and the second links to a `static` question. There is only one `link as duplicate` because Stack Overflow only allows one `'duplicate` flag per question – VA systems engineer May 20 '18 at 10:19
  • They're you're links, read them. @NovaSysEng, here's a hint. C is not an OOP language. No encapsulation, the thing that Static and this specifically exploit within C#, making them very different things in the VERY different languages. – Confused May 20 '18 at 12:59
  • @confused: the code in the question looks like C# and it is tagged with C#. Take a chill-pill – VA systems engineer May 20 '18 at 13:53
  • I'm not the one attacking the question as being a duplicate, and being completely wrong about that claim. You are. Either retract your nonsensical, inaccurate attempt to suggest dismissal of this question as a duplicate, or expect to have your judgement challenged... or read the posts you're linking to in support of your "argument". Chill out... yourself. Don't attack someone's question unless you're at least partially sure you're right in your claims. There's more than enough policing of questions going on, no need for extra policing that's wrong. – Confused May 20 '18 at 14:17

1 Answers1

4

this is a C# keyword that is a reference to the current instance that the code is running in.

static means there's only one memory location for this variable, meaning even if you had a second instance of this class, this variable would point to the same backing data.

Used together, this is the "singleton pattern." See here for some extra info.

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37
  • i used google and watched tutorials, docs and stackoverflow comments too but they didnt let me understand these lol –  May 19 '18 at 10:17
  • @Kristóf instead of googling you could try watching on youtube some basic tutorial about c#.. just saying . :) – Ginxxx May 19 '18 at 11:37
  • Need to find better tutorials then. Or instead of watching videos, read a good c# book. What ever happened to reading books? Am I that old? – pinkfloydx33 May 19 '18 at 17:50
  • Why complain about this question? At no point, in watching and reading and trying C# tutorials have I seen a better, more succinct differentiation and definition of this and static. And it worked for the OP, too. There is no static, worthwhile instance capable of solving this dilemma as well as this single instance. And there's no accounting for what bits of conflation and/or contradiction in purpose and ability, or coincidental and complimentary capability will trip up anyone. Especially new learners. I think the OP deserves credit for asking so clearly, too. Everyone stumbles somewhere. – Confused May 19 '18 at 18:03
  • By way of example of the significance of good reference materials, take this: https://stackoverflow.com/questions/572547/what-does-static-mean-in-c?noredirect=1&lq=1 it's not the same in this language. At all. But what's really interesting about this question... that it a static and this, combined, provide incredible power, that I'm only somewhat aware of thanks to this answer. There are ways to improve this question, now that I see what this and static can do together. They seem as powerful as any feature of OOP, but go against it, when together. Beautiful. Paradigm levels of intriguing. – Confused May 19 '18 at 18:09