Hey guys,
I’m still relatively new to programming, so I’m just trying to make sure I’m doing this the right way. I have a move class that moves my player around. When I run into an enemy, I attack that enemy. My PlayerAttack class is not a mono behaviour, and looks like this:
public class PlayerAttack
{
public LivingObject MeleeTarget;
public PlayerController MyPlayerController;
public LivingObject PlayerLivingObject;
public PlayerAttack(PlayerController p)
{
MyPlayerController = p;
PlayerLivingObject = p;
}
public void Attack(LivingObject MeleeTarget)
{
//attack stuff here
}
}
In my movement script, I create a player attack instance with:
private PlayerAttack playerAttack = new PlayerAttack(playerController);
And when I want to attack, I use:
playerAttack.Attack(MeleeTarget);
Is this the correct idea? I make the object and call it when I need it? Should I be making some sort of static class instead or just creating when I use it?
Man this programming stuff is hard 
1 Like
Well, one of the great things about programming is that you can accomplish the same task in multiple different ways.
Sure, there are better ways to go about implementing very specific things, for example, this:
void Update() {
//"Camera.main" returns a FindGameObjectsWithTag method call. This is happening every frame.
Vector3 mainCamPosition = Camera.main.transform.position;
}
Versus the same thing, but without a FindGameObjectsWithTag
call every frame:
private Camera mainCamera;
void Start() {
//Caching a reference to the main camera means a FindGameObjectsWithTag call only happens once.
mainCamera = Camera.main;
}
void Update() {
Vector3 mainCamPosition = mainCamera.transform.position;
}
But when it comes to entire features, there isn’t really any “objectively correct” way to implement them.
When working with a team, you would typically use some standard that you and your partners all agree on.
When working solo, you can pretty much just implement a feature in whichever way you feel comfortable.
1 Like
You’ll run into a lot of opinions about this.
Best practices and all. 
Cucci_A’s recommendation is mostly correct.
There are basically 2 simplified reasons for best practices;
#1 - It makes the code/program much easier to extend and work with.
#2 - Anal Retentive programmers.
I typically fall in #1, and have colleagues who fall in #2.
That said, a couple answers:
Avoid static classes where possible.
A static class should ONLY be used when you absolutely need only 1 of ‘the thing’, and have no need for duplicates.
That is an over-simplified explanation, but static classes are often used in a Singleton Pattern. (Decent for gaming)
Now, to address your question.
You can do you attack as a separate class, and call that class as needed, which will work just fine.
A setup like this is often called using a Processor, or a class that ‘processes something’.
Declaring that in your Movement script is perfect, that way your movement script has something to call.
You’ll end up with a lot of separate classes doing this, but each class will contain a smaller amount of code and be easier to edit/update.
Another approach would be to add a method in your movement class.
This approach leads to larger chunks of code in classes.
Both are acceptable, depending on your preference.
1 Like