Please some explaining c#

I really am struggling with the C# code, never am I able to finfish the challenges.
So I try to break down some coding, here is maybe a bit of a silly question.

    void Update()
        timerImage.fillAmount = timer.fillFraction;

My question is about timer.Fillfraction, is it calling for the fillFraction in the timer.cs script here?

Hi Amanda,

First of all, there are no silly questions as long as you are genuinely interested in the answer.

Regarding your question and since you asked: The answer is no, even though you are not completely wrong. You seem to have the right idea but I am not sure if we mean the same.

Let me explain:

Timer is the class. A class acts as a blueprint for objects. Just imagine a class/blueprint named Car. Are you able to drive a blueprint? No, you aren’t. You need an object. In Unity, we create an object of a class by attaching the script to the Inspector of a game object. Each object has got the “things” that were defined in the class. A Car object has got wheels, it can drive (hopefully). The Timer object has got a variable named fillFraction.

timer is a variable of type Timer. We are able to assign an object reference (“link”) to the variable. The object must be of type Timer.

If this made sense, rephrase your question, then my answer will very likely be “you are right”. :slight_smile:

If you just want to read "the solution", please click here.

fillFraction in timer.Fillfraction refers to the fillFraction variable in the Timer object which is assigned to the timer variable.

This might sound like nitpicking but the difference between script, class and object is crucial in an object-oriented programming language such as C#.

Did this clear it up for you?

See also:

Hi Nina,

Thank you for your reply, it made some things clear :slight_smile:

But what I don’t understand is like we declare Timer with a capital T to timer, why? Is it that Timer is a function of it’s own, try to find it on the MS page for C# but didn’t come up with a clear solution.

I’m not going to panic (yet), still have another C# course laying on the shelf and hope to learn some about it.

Just don’t see the link, when I read the code it makes sense to a certain degree but missing to much knowledge to see the big picture.

We are following the/a C# naming convention. This “requires” us to have classnames start with a capital letter.

public class Person

And the variables inside the class usually start with a minuscule letter, except for the primitive types and the three built-in reference types string, object and dynamic.

public class Person
    string name;
    string role;
    int age;

    Person mother;
    Person father;
    Person[] siblings;

In C#, the syntax for the declaration of a variable is:
accessModifier classname/type variableName;

We have a Timer class, so a variable of type Timer must be declared as:

Timer timer;
Timer myTimer;
Timer anotherTimer;

// with an access modifier
public Timer timer1;
private Timer timer2; // private is the default access modifier

A convention means that a group of people tend to follow these rules. The compiler does not care if you have your classnames start with a minuscule letter.

class person
    string name;
    string role;
    int age;

    person mother;
    person father;
    person[] siblings;
    person[] Friends;

That’s valid C# code but “bad practice” because it contradicts the majority of the C# naming conventions. It makes understanding the code difficult as it goes against the expectations.

1 Like

Timer is a class. The real reason it’s declared with a capital T is it’s called Timer with a capital T. Within Timer, it’ll have methods (basically Functions, but they’re called methods inside a class).

In C#, there are certain standards (it’s not quite the right word, but the meaning is there). Think of it like standard operating procedure. Each programming language has its own way, but in C# it’s pretty much expected that a class starts with a capital letter. It doesn’t mean you have to, the c# language itself will let you name your own class with lowercase, but I wouldn’t if I were you, it’ll make you life harder in the long run if you do.

Edit: Nina beat me to it. lol

I think I got it now.
Just to see if I got it right.

Timer timer;

It means there is the cs script called Timer, to call functions within this script it’s necessary to declare it as timer.
Same as: ScoreKeeper scoreKeeper, we can call the functions from ScoreKeeper within another script with scoreKeeper.

Please tell me I’m right here :slight_smile:

@MichaelP, hope Nina didn’t beat you up to hard

There’s some other nuances in there, but you pretty much got it right.

Oh, sometimes she’s brutal :wink:

1 Like

Baby steps, I get there is way more going on.

I’m sure going to study way more on the C# subject and probably spamming everyone until it has been beaten in to me :wink:

1 Like

No worries, I’m just barking. :wink:

@Amanda73, if you have many questions on C# and would like to focus on this programming language for a while, I can recommend Bob Tabor’s free C# course.

1 Like

Thanks, that’s a good one.
I wan’t to understand what and how I can work with C#, just typing over or copy and paste what others make isn’t the same. Just want to learn to build something from scratch and I do have the feeling it could be done with the right knowledge…

I have learned in the past about music theorie, not just like it’s just this way but why and how to apply.

That’s what I aiming for to learn in combination with Unity.

Thx for all the help.

In this case, definitely do take a look at Bob’s course. It will answer a lot of “why and how to apply” questions. With a solid understanding of C# and programming in general, accomplishing things in Unity will become much easier. Just as it is easier to compose a nice piece of music if you are familiar with music theory.

I have read your bio, just had to smile. Like basic, so much fun but loading from a tape or just typing from a book, after like 2000 lines RUN, you get this syntax error in line 730 :rofl:

Btw image from my first game console the year 1980

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.