Getting a variable works in a Start function, but not in another one.
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 !
You should also show us the script that isn't working. :)
Did you re-declare the movementSpeed variable in the script that's not working? This is a common error I see people do:
//field
int movementSpeed;
void Start() {
//local variable, no relation to the field
int movementSpeed = GetComponent<VariablesScript>().movementSpeed;
}
I get the movement speed in the control script, and health in the status script.
The code in the status script is :
int health;
void Start() {
health = GetComponent<VariablesScript>().health;
}
Exact same code than in the control script, but for another variable, in another script. (Note than the variables script, the control script and the health script are all attached to the player prefab, that's why I can't understand why it's working for only one of the two attempts..)
Just double checked, I didn't re-declare the variables, good thing you ask, that's an error I'm still doing sometimes..
Are you adding VariableScript to the gameobject in the editor? Or do you add that at runtime? If at runtime then script execution order would be important - it would be possible for your health script Start function to run before VariableScript was added.
If that seems like it might be the case, you can control script execution order. $$anonymous$$ake sure whatever script adds VariableScript to the gameobject runs before anything that uses it.
Or just use Awake on the VariableScript, as that runs before Start.
Yes, or Awake on whatever script is adding VariableScript (if that is indeed what's happening).
Answer by YumeYume · Sep 11, 2015 at 06:31 PM
Thanks for your replies guys, but as I thought, that was a stupid error that I did completely out of the script..
My game is a multiplayer game, and I'm using UNet to handle the network. Of course, instantiating multiple instances of the same prefab with the same scripts would lead to problems like controlling all players because the script is enabled for all objects on all clients. To avoid that, I just have a script to enable the scripts for the local player only. Control and health scripts are in this list. But I forgot something for the health script.. They need to be disabled at start, and then enable them with the script. And I forgot to uncheck the enabled case in the editor for the health script.. A single click that changes everything.. Now all is working. Sigh.. Tired of doing newbie mistakes. Thanks for the execution order link though, I still have a lot of things to do, this link could be useful in the future. Thanks again !
Your answer
Follow this Question
Related Questions
Getter working, Setter not 2 Answers
Class array initialization on Start() gives -> Object reference not set to an instance of an object 1 Answer
How to create static component? 0 Answers
Constructor always creates null object --why? 0 Answers
Wave Spawn Script not working? From Null to Destroy Object. 0 Answers