#csharp basics
1 messages ยท Page 1 of 1 (latest)
hi
i'll use your screenshot to refer to
so c# is what's called "Object Oriented Programming"
which is to say that
you're gonna be making a lot of objects and then using them to do stuff
Makes sense
maybe
If it's comfortable with you we can do it real quick
csharp basics
sure
i'll stay here though
tutorials can be a bit of a grab bag in that way
but i wanna kind of go through it all and be thorough
so you can take those concepts and continue
that's okay :)
so im gonna try to cover everything in the screenshot
so that you can understand what it all means
so:
c# is an object-oriented language like i said
you wanna make objects.
to do that, you start with a class
a class is a description of a kind of object
so at the very top of your script you'll see public class Player : Monobehaviour
so a class is like template
it describes a kind of object.
so if i wanted to make like a coffee cup
i'd say class Cup {}
a class is more like a description of what something is.
yeah, you can make a spear
public class spear {}
and then inside the {} is where you put all the details.
yep
()
we'll get to that soon
yeye
the curly brackets are like being 'inside' something.
anyway, once you have an object, you can give that object properties. so for example
in a spear, you might want to adjust it's length
so the spear would have a length property
your properties can be anything, for example my coffee cup could have a property that says how much water is in it.
maybe not yet , as long as you kinda understand
you can use any kind of software i think
ok so
what's important is that your class describes what an object is and what it does.
you can make lots of classes
so when i have a class spear, i don't have an actual spear, but it's a description of what a spear is and how it works.
but you can make actual instances of that class.
yeah
yep
let's say i had two copies of the book Honeybee and i threw one in a lake
those are two different instances of Honeybee, because one of them got thrown in a lake and the other didn't
but they both contain the same words, and this comes from the class
they're different in the sense that two starbucks coffee mugs are different coffee mugs even if they are both copies of the same kind of mug that starbucks made like 10000 of
another way to look at it is: the Class is the factory and the Instances are the objects that factory creates
does that make more sense?
okay so public class Player : Monobehaviour
public is your access modifier (this doesn't matter right now).
class means you're making a class
Player is the name of the class that you choose
monobehaviour doesn't matter.
these are two variables again
a variable is like a bucket. you can put information in the bucket and save it for later.
and you can look in the bucket to see the information
that's right
yeah
you use it to store data.
so private bool jumpKeyWasPressed;
private is the access modifier again (it doesn't matter right now)`
bool is the type of information that you're storing
and jumpKeyWasPressed is the name of the variable that you chose.
yep~
bool can be true or false.
float stands for 'floating point' and it's used to store a decimal number.
like 0.5;
well 1 is the same as the decimal number 1.0
so a float can store any number you want. it's how you store numbers basically.
if you want to store a number you put it in a float.
if velocity is a number, then yes :)
float x = 1;
x = x + 1;
// x will now equal 2```
that's right!
sometimes!
you want variables so that you can store the information and act on it later.
yeah.
unfortunately
games are super complex ๐
this is the basics
yeah there is
yeah, they do typically
but you can get into the mindset of it.
anyway there's one thing you'll see
possibly
still on our screenshot here,
like i said, float is a type of number. you might also see double, which is another kind of number, and they look the same.
a double looks like 1.0;
so to tell them apart, floats have f after them.
and this is what 1.0f means in your code
so f just means it's a float.
so Input.GetAxis() points to more code somewhere else.
it's known as a method, distinct by the () parens.
when you run a method, it goes off and calculates something, and then it gives you what it calculated.
so here Input.GetAxis() calculates something and then that something is returned to horizontalInput.
does that make sense?
that's right!
the equals sign = is assignment.
it makes the thing on the left side equal to the thing on the right side.
so if you go x = y, then whatever was in y is now in x.
that's right
yep
yeah
and more reusable
you can lay things out however you like
methods and variables are different, though, keep in mind.
a method has ()
a variable doesn't
that's how you tell
It's also common to name methods with CapitalLetters
but name variables with lowerCaseLetters
a variable only stores information, but a method can take actions and do things.
yep
so i wanna get you to do somethin
in unity can you make a new empty object
you don't have to make a new project btw you can do it in your other project
nah that's not true
ppl are making bespoke things 90% of the time
btw
when you see people say the word properties or members
those are just other names for variables, it's the same thing
complicated historical reasons
similarly, method and function mean the same thing
yeah
what u gotta get about games though is that everything is a variable
like; in your jumping game, the position of the player is a variable. the game knows where to draw the player graphic because that information got stored in a variable.
anyway on your cube
delete the collider
we don't need it
and go Add New and we're gonna make a new script
you can call it myScript ig
nvm u got it ๐
so the Update method is the part from void Update(){ to the next }
and we're gonna delete it
we're gonna delete the start method too
so now we'll just have ```csharp
public class Cube : Monobehaviour {
}```
and if you save this and check in your game, you'll see:
... a component with nothing in it
yep
this is what we expect, because we didn't add any variables yet.
so if you add float x inside the method body, and save, you'll see it show up in the component
public float x;
you can decide what you want it to be ig
0 is fine for now
your screen is super blurry sometimes lmfao
its better
so your variable declaration is incorrect there
the declaration is the access modifier, then the data type, then the name. instead of the name you've got a 0
so public float 0 should be public float name
:)
i mean it's red cos it's only half written so
dw about that
man there's like
so much
to cover
but in the meantime
just write Input.GetKeyDown("");
and then click on it and press ctrl + full stop
ok yeah your IDE isn't connected sjkbhs
that's a problem
dw
at the top add a line Using UnityEngine.Input
make sure to end your statements with ;
okay and i see that didn't work so my memory's fail;ing me i guess
i gotta open unity myseelf
ok so make sure to fix public float 0 because your IDE will get stuck there and won't highlight anything else until that's fixed
KeyCode isn't a method, you have to do KeyCode.X;
well i wanted to show you methods
so inside the class, add ```csharp
public void Test() {
x += 10;
}
methods are the way you do things, and here the thing we're doing is adding 10 to x.
so yeah can you see how the method's gonna work
when you call Test(), it will run the code in between the {}
[ContextMenu("Test")]
public void Test() {
x += 10;
}
change it to look like the above now.
blurry screen again sbhkljs
ok so you've got an extra } where it shouldn't be
actually if you click the very first {
it will highlight the matching }
so if you do that, you'll see that right now, public float x; is hanging outside of the block in no-mans-land
it needs to be inside the class
perfect
save that and go back to your game
and on your component now if you click the 3 dots menu you'll see Test
i cant actually see if that worked but i assume it did
so congrats u just called a method!
ok ur havin problems here and i'll explain why
so we made a class called Cube
capital letter, Cube
capital letter Transform, is also a class. thatt is to say: it's the blueprint of the object, not the object itself
so you want lowercase transform
it can, the IDE is just aggressive lol
the line you want is transform.scale = transform.scale*2;
so remember that it's only the methods that can do stuff
so if you want to do a thing you have to do it inside the method
and right now your line is inside the class, but not inside the method. the method is Test() {}
public void Test() {
/* this is inside the method */
}
so we gotta pause a little here
so take a look at your code and see the symbol x
what is this? The code doesn't know, because you haven't said.
i will see soon enough sbkhjsbskhjbs
ok so you've got the right idea
we need to tell the code that x is a number
specifically a float
so public float x; in the class somewhere will make that go away.
(highlighting won't work right when you have errors)
okay so you're trying to use KeyCode like a method, but it isn't one
remember that () is for methods
GetKeyDown is a method, it's just KeyCode that's not.
discord deleted my message?
ok i'd better explain .
Input has a bunch of things inside it. The dot is used to go inside it and grab one of those things.
So if you write Input. it'll show you a list of all the things inside Input
These things can be variables or methods
Input.GetKeyDown()
^ this is grabbing the GetKeyDown() method that exists inside of Input.
KeyCode.X is grabbing the X property that exists inside KeyCode ( property, not method, so no (), because () is for methods)
it's just data
anyway this is good but you just gotta fix the problems.
you need public float x; in the class
yeah so fix that
if you look at the code tho you'll see you haven't written public float x;
and it's gotta be in the class
not in the method
you're right,
you're right to think that but it doesn't work like that
Test() is inside the class
but if you put something inside of Test(), that's different
so it needs to go on line 7
that's correct now
perfect
there's so much to cover, i'm realising now
i need to go pretty soon
but yeah tthat video tutorial you had
seems really good, and it's so big because there's just so much to cover.
but i'd say you should sstart from the top iff you get lost
it's the box next to the Albedo label
you may be a able to get someone else to continue on with
i wanted to show method paremeters next
Respect.