C# Question regarding Levelmanager/LooseCollider Scripts

Hi all,

before I continue my course I would like to understand everything I have seen so far. There is this one line of code, that I don’t understand from the synthax point of view. I fully understand it’s function and need though, but I only have beginner knowledge in C# and in that case it does not make much sense.

In “Loose Collider” script for the ball, we have added this line :

public LevelManager levelManager;

I get it, that we are making the LevelManager-Script accessable from within the LooseCollider-Script, but in c# terms, what is this ?

Are we creating a new class here, or a variable called LevelManager and then assign it a internal variable called “levelManager” ? Or is this just a C# way to activate other scripts within the current script ?

Also, what jumped me, we are calling the variable “levelManager” within the looseCollider-Script, to access the LevelManager-Script. That’s fine. But why is it, that in Unity GUI itself in the inspector of the Loose Collider Script now the variable appears as “Level Manager”… I have nowhere written it with a space, where does this come from ? It easy to follow in this simple example, but in more complex projects this behaviour could be misleading. Where does it come from ?

Thanks for explaining :slight_smile:

B/R
Michael

Hi Michael,

To answer your questions;

Are we creating a new class here, or a variable called LevelManager and then assign it a internal variable called “levelManager” ? Or is this just a C# way to activate other scripts within the current script ?

The line in question;

public LevelManager levelManager;

…is made up of three parts;

  • public - the access modifier
  • LevelManager - the data type of the variable
  • levelManager - the name of the variable

The access modifier determines the scope of the variable/object, e.g. what else can see/access it. In this case it has been set to public, so from the point of view of this project, everything, and that will include the Inspector.

The data type LevelManager is the type of the class, exactly the same as string or int, in this case it is a class that you have defined yourself with the LevelManager script.

The variable name, levelManager, could in fact be called anything you like, but it is good practice to name objects based on what they are.

The reason for having this is so that the LooseColider script can access the LevelManager, you have a variable of the same type (LevelManager) to store its reference in. By making it public, it is accessible within the Inspector and you can drag your LevelManager GameObject from your Hierarchy into this exposed field. Within the code in your LooseColider script you can then access the functionality of the LevelManager via the variable, for example;

levelManager.LoadLevel("Win");

In the above, we are accessing a method of the LevelManager to load another scene.

But why is it, that in Unity GUI itself in the inspector of the Loose Collider Script now the variable appears as “Level Manager”… I have nowhere written it with a space, where does this come from ?

This is a feature of Unity, it is trying to help you, it parses the variable, property, and methods names to make them more readable in the Inspector. It is infact still levelManager behind the scenes, it’s just put a space between the words and upper-cased the first letter to make it a little more readable for you.


See also;

1 Like

Hi Rob,

thank you very much for your good explanation here. While everything is helpful, the big insight was that an already existing class can be a datatype by it’s own. Like I said, I only have performed absolute beginner classes in C#, and I didn’t know what type of data types are even possible, i thought it limited to “bool, int, double etc”. Making entire existing classes a datatype offers me a much bigger way of thinking :slight_smile:

Regarding the last part, the writing style of “Level Manager”… i actually have to try that out in bigger projects i will create in the future. To be honest, in my way of thinking it’s not really a good feature of unity creating a “fantasy name” here, as a gamemaker AND coder, i would find it more convinient if it would refer to the names that I have definied in my own written scripts. But again, that might be a much subjectiv impression, for whole teams working on some projects, that could become helpful.

Thanks again, and have a nice day :slight_smile:
B/R
Michael

You’re more than welcome Michael :slight_smile:

I’ve popped a couple of links below which may also be useful, either now, or as you progress regarding value types and reference types.


See also;

1 Like

That’s great, thank you again :slight_smile: Sometimes I feel intimidated by the complexity of the entire c# field, but then again, I think it’s realistic to become a small gamedev without becoming a super level programming guy before :slight_smile:

You will be surprised how much can be accomplished with the C# that is covered in this course, from a games perspective.

Whenever you can, try to expand on the course version of the games, make a list of a few features that you think might be nice to add and then have a go at adding them. If you get stuck, just ask, it’s a friendly community here and there are lots of people willing to help. Nothing quite beats the sense of achievement of making these games unique to you, and if you have friends/family to share them with also you are certain to bring a smile to some faces :slight_smile:

Will do :slight_smile: But I hope I can switch as fast as I can to my own project and learn the rest with “learning-by-doing-(mistakes)” :slight_smile: , but I really need to get myself on a leash here and not rush forward too fast. First, I will finish this course, then I will do the Unity Course 2.0, and 2 others I booked on Udemy. And maybe in the meantime, there will be a new course for turnbased strategy like Ben is currently trying to research if there is enough interest out (and it does seem to be that way :wink: )

1 Like

Sounds like a great plan - keep us updated on your progress and please do consider sharing the games you make :slight_smile:

Count on it :slight_smile:

1 Like

Privacy & Terms