Hey @Matt_Beebe . Here are my guesses to your questions :
-
OnDestroy is also called when you load a new scene (ex: a new level). I guess this would be useful if, for example, in Lvl 1 - you are on the ground and have a specific Control Scheme, and in Lvl 2, you are driving/flying, and you want a different control scheme. It’s always helpful to read the unity docs about built-in functions and what they do. [Unity OnDestroy]
-
I think it makes more sense to write the shortest amount of code that’s the easiest to read and handles the most exceptions. In this case, if you would start with your If statement with what you want to do, there’s a higher chance you will forget to place the else at the end of the return statement. There’s also the added complexity if you want to check multiple flags before doing something; in that case, I think you will end up with duplicated code
Example 1
public void OnJump(InputAction.CallbackContext context)
{
if (!context.performed) { return;}
JumpEvent?.Invoke();
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed)
{
JumpEvent?.Invoke();
}
else
{
return;
}
}
Example 2
if (stateMachine.Targeter.CurrentTarget != null && stateMachine.InputReader.IsTargeting) {return;}
if (movement == Vector3.zero) {return;}
FaceDirection(movement, Time.deltaTime);
In example 2, I think you would need to write "FaceDirection(movement, Time.deltaTime); " in both checks, which would add a considerable amount of extra code lines in the long run.
My best advice - Get the free trial of JetBrains Rider - Rider: The Cross-Platform .NET IDE from JetBrains
The amount of suggestions it gave me to improve my code is fantastic (compared to Visual Studio)