How to get methods through classes?
I have three diffrent classes in a simple game im coding, One class had way too much code so i made another one however the method is not getting recognized. Am i doing osmething wrong??
43 Replies
What's the error?
That's because the method is a member of a different class
yes i have it in another class
You can make that method static and reference it with class name
Or keep it non-static and use an instance
but if i make it public?
would that work
Then... it will be available from outside of the class, yes
how do i do that?
But still a member of it
static
modifierlet me try that
why do i get errors?
Static methods cannot reference non-static members
I would recommend learning C# and object-oriented programming in general before jumping into Unity tbh
this is monogame
Still
im trying to learn it from here for a school project
so it becomes easier
but we bearly gor any good lectures
You're trying to learn swimming by jumping off a cruise ship in the middle of an ocean
Start with $helloworld
Learn what classes are
honestly im just trying to get this porject done then get more into coding
since we bearly have anytime to learn
or understand the classes
Then drop the idea of splitting your code up, it will make things easier
Or use partial classes
i have to split it into 5 diffrent classes to get a grade
Split this very code into 5 classes, or just use 5 classes?
You need to treat each class as its own, distinct, thing
With its own, distinct, properties and methods
i do know that but the whole project has to be in 5 classes. my teacher did a similar thing
where he put methods in other classes
For example, a
Car
class won't use methods from a Toaster
class, it makes no sense, they're completely separate and orthogonal
You can have some code depend on other code, yesand i cant ask for help since it would l ower my grade
A
Car
can make use of an Engine
class
Since an engine is a part of the carso it inhertis?
inhertis from another class
But just splitting a car into
LeftCar
and RightCar
makes no sense
No, not talking about inheritancethats the thing im just trying to get to 5 diffrent classes so i can get a good grade
even if its useless and i know its dumb to do that
im just doing it for the school project
Talking about
See how
Engine
and Light
are components of Car
?yes i can see that
I mean u should have that easily, u have the player, u have the main game class. u have a screenmanager, u have a scene no?
i have all of that
those are all classes
im just trying to get a method from a class to another class and have it still working
but im getting an error
You can't just move things willy-nilly
Each class has its own members
Each is its own, unique and separate, microcosm
You can have static members in class
B
and use them in class A
, yes
But that method will have no knowledge of anything from class A
, ou will have to pass it some parameters
You can have a method in class B
be non-static, which means it's an instance method
And that method can only be called on an instance of class B
You can't just "move some methods"
You can't move the "heat" functionality of an Oven
into an UngaBunga
if i change my varibles to Static isntead and make the method static in one class can i then use it in another class without it effecting my current code`?
If you make everything
static
you will have a pile of crap and not codethats the thing i dont get. if i have 5 classes and each class dosent know whats going on in the other class how can i make a game?
* Inheritance —
Car
inherits Vehicle
* Composition — Car
has an Engine
Two ways
Pick your poisonit woudl be so much easier ifi made it into one class but that woudlnt be acceptale by the teacher
i will look more into this and ask the teacher about it , maybe even get some help. But thank you for explaning it i really appreciate it