Sorry if the title isn’t that clear.
I’m making a multiplayer shooter game, and now that the game is mostly playable (though not fun in the current state), I’m updating my code to introduce modularity. As I want the players to be able to choose a character with different stats, I made a class that adapts those stats in function of the user’s choice, then I get these values in the scripts that needs them, so I can add more characters over time without having to modify the controls/health/etc scripts each time.
The class that contains the characters uses variables like this :
public int Health { get; set; }
And a function set these variables according to user’s choice.
Then, in the game scene, I get these variables using a GetComponent from the script where I need them. For my Player Controller script, it works perfectly, I tried several choices and it worked every time.
I proceed like this :
//variables
int movementSpeed;
void Start() {
movementSpeed = GetComponent<VariablesScript>().movementSpeed;
}
//then I use movementSpeed in the script, and it works
I did the exact same thing in my Player Health script, getting the max health the same way I do with movement speed, but for unknown reasons, the get isn’t working, my variable is equal to null.
I tried to get the variable in the update function, and it’s working, but of course, I don’t want the get to occur every single frame, it’s just for initialization…
I don’t understand why the exact same thing works in a script and not in another… Any clues about why a get would work in a place and not in another ? Thanks in advance !