C
C#4mo ago
TOLOXI

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??
No description
43 Replies
Angius
Angius4mo ago
What's the error?
TOLOXI
TOLOXI4mo ago
No description
Angius
Angius4mo ago
That's because the method is a member of a different class
TOLOXI
TOLOXI4mo ago
yes i have it in another class
Angius
Angius4mo ago
You can make that method static and reference it with class name Or keep it non-static and use an instance
TOLOXI
TOLOXI4mo ago
but if i make it public? would that work
Angius
Angius4mo ago
Then... it will be available from outside of the class, yes
TOLOXI
TOLOXI4mo ago
how do i do that?
Angius
Angius4mo ago
But still a member of it static modifier
TOLOXI
TOLOXI4mo ago
let me try that
TOLOXI
TOLOXI4mo ago
No description
TOLOXI
TOLOXI4mo ago
why do i get errors?
Angius
Angius4mo ago
Static methods cannot reference non-static members I would recommend learning C# and object-oriented programming in general before jumping into Unity tbh
TOLOXI
TOLOXI4mo ago
this is monogame
Angius
Angius4mo ago
Still
TOLOXI
TOLOXI4mo ago
im trying to learn it from here for a school project so it becomes easier but we bearly gor any good lectures
Angius
Angius4mo ago
You're trying to learn swimming by jumping off a cruise ship in the middle of an ocean Start with $helloworld
Angius
Angius4mo ago
Learn what classes are
TOLOXI
TOLOXI4mo ago
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
Angius
Angius4mo ago
Then drop the idea of splitting your code up, it will make things easier Or use partial classes
TOLOXI
TOLOXI4mo ago
i have to split it into 5 diffrent classes to get a grade
Angius
Angius4mo ago
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
TOLOXI
TOLOXI4mo ago
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
Angius
Angius4mo ago
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, yes
TOLOXI
TOLOXI4mo ago
and i cant ask for help since it would l ower my grade
Angius
Angius4mo ago
A Car can make use of an Engine class Since an engine is a part of the car
TOLOXI
TOLOXI4mo ago
so it inhertis? inhertis from another class
Angius
Angius4mo ago
But just splitting a car into LeftCar and RightCar makes no sense No, not talking about inheritance
TOLOXI
TOLOXI4mo ago
thats 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
Angius
Angius4mo ago
Talking about
class Engine
{
public void Start(){}
}

class Light
{
public void TurnOn(){}
}

class Car
{
private Engine _engine = new Engine();
private Light[] _lights = new[]{
new Light(),
new Light(),
};

public void Run()
{
_engine.Start();
foreach (var light in _lights)
{
light.TurnOn();
}
}
}
class Engine
{
public void Start(){}
}

class Light
{
public void TurnOn(){}
}

class Car
{
private Engine _engine = new Engine();
private Light[] _lights = new[]{
new Light(),
new Light(),
};

public void Run()
{
_engine.Start();
foreach (var light in _lights)
{
light.TurnOn();
}
}
}
See how Engine and Light are components of Car?
TOLOXI
TOLOXI4mo ago
yes i can see that
leowest
leowest4mo ago
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?
TOLOXI
TOLOXI4mo ago
i have all of that
TOLOXI
TOLOXI4mo ago
No description
leowest
leowest4mo ago
those are all classes
TOLOXI
TOLOXI4mo ago
im just trying to get a method from a class to another class and have it still working but im getting an error
Angius
Angius4mo ago
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
TOLOXI
TOLOXI4mo ago
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`?
Angius
Angius4mo ago
If you make everything static you will have a pile of crap and not code
TOLOXI
TOLOXI4mo ago
thats 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?
Angius
Angius4mo ago
* Inheritance — Car inherits Vehicle * Composition — Car has an Engine Two ways Pick your poison
TOLOXI
TOLOXI4mo ago
it 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