I have a public variable killx
in World on player. In a different script, Block, I am trying to set it like player.GetComponent<World>().killx = (int)Mathf.Round(gameObject.transform.position.x);
but this doesn’t work. If it did, a function would run. I have changed the variable manually and it does work then. So the problem lies with the Block script setting the variable. This line of code does in fact run according to the debugger.
Probably need more information than “not working”.
You also shouldn’t blindly use GetComponent<T>
like that, as you’ll run headlong into null-reference errors. TryGetComponent<T>(out T)
is the better choice these days.
Sure, but it’s not running correctly so tear that monstrous line of code apart and find what the data is, the actual values. Remember: code doesn’t matter. Code exists only to transform data. What is your data doing? Find out!
If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.
Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.
The longer your lines of code are, the harder they will be for you to understand them.
How to break down hairy lines of code:
Break it up, practice social distancing in your code, one thing per line please.
“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums
“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums
I’m glad you said that. I think this will really help me in the future. I had posted another question because of null-reference errors and this may have been the cause.