- Home /
Why does my Physics.gravity revert value change when accessed by one script, but not another?
Wow, that's a verbose title.
Physics.gravity = -other.transform.up * GRAVITYCONSTANT;
This is the line I'm using in one script, which simply doesn't work. looking at the data of the debug log, it tells me that Physics.gravity sometimes lets itself be changed for a second, but almost immediately changes back to what it was before. However, in another script with this line:
Physics.gravity = direction * GRAVITYCONSTANT;
it doesn't matter how often I call that line, it causes no problems. I've tried referencing the working script's functions that uses that line, but that does exactly the same. It's driving me batty.
Are you trying to change the gravity of different gameObjects or are you trying to change the default gravity for the game? If it's the second one you are trying to do, where do you have these lines of code at in your game?
Physics.gravity = -other.transform.up * GRAVITYCONSTANT;
Physics.gravity = direction * GRAVITYCONSTANT;
In the first it's in a OnTriggerStay function. The other is in its own function in another script and called every frame in an if statement. I've also tried calling the second function in the first script, but nothing. I've now combined the scripts into one, which didn't work at first, but after a while a part of the script got an error without me touching it, so I altered it, and now everything is working with me having no idea why. Then again I had no idea why it wouldn't.
OnTriggerStay and in Update? Physics callback functions happens before the Update functions. So it sounds like to me that you are just setting gravity in the OnTriggerStay and then immediately setting it back in the Update. Impossible to know for sure without the entire scripts.