What I want to do is to have a variable in my movescript, thats default value can be changed from the GUI. But the trick is that I also want the variable to be changeable from other script.
What I mean is that I have the default vaule of gravity that can be set from the GUI, but when I enter water, which is surrounded by a box collider, which has Is trigger enabled, then a script attached to the collider changes the value of the gravity to zero, so it feels like water and when I leave the water the gravity is applied again.
But my problems are: When I set the gravity value to static var, then I can access it from the other script, but I cant see it from the GUI anymore. Also I cant change the value of the gravity from the other script. Because the gravity still remains same.
Can anyone help me?
Static variables can’t be set in the editor UI (I’m assuming that’s what you mean by the UI). They are akin to global variables as the reference to them is not stored per instance of a type, but for the type itself.
You can use gameObject.GetComponent() to get an instance to the script on a transform like so:
YourClass yourClass = someTransform.gameObject.GetComponent<YourClass>();
if(yourClass) // If there is no YourClass component on the gameObject then this returns false
{
yourClass.gravity = 0;
}
Regards
/Klum
This is correct.
You can create a dummy public var and, on Awake(), set a static var with this value as long as this value is different than zero (if you need some checking).
So you’ll be able to edit the variable through the Inspector, and you’ll also have it as static.
What do you mean by this? When you set the gravity to static, and you change it from another script, it remains the same? That doesn’t sound right.
If the gravity is not static, then when you change it you’re only changing that variable for that instance only of that script.
well I solved the problem with changing the variable from UI, as you said, but about the static variable:
I did it so that in one script I have:
static var gravity:float;
var grav:float = 20.0;
function Awake () {
gravity = grav;
}
And in the other script it should go to zero when I enter the box collider (at least as I understand)
function OnCollisionEnter(collisionInfo : Collision) {
WalkerMod.gravity = 0;
}
(WalkerMod is my moving script and the other one is swimming script)
I almost solved the problem…
This is my swimming script:
var upforce = 10;
static var forceup;
static var swimming:boolean=false;
function Awake () {
forceup = upforce;
}
function OnTriggerEnter(other : Collider) {
// swimming
swimming = true;
}
function OnTriggerExit(other : Collider) {
// not swimming
swimming = false;
}
and this is how the movement script treats it: (no underwater special movement yet)
var gravity:float = 20.0;
private var swimming:boolean = false;
private var defaultgravity:float;
function Awake () {
defaultgavity = gravity;
}
function Update ()
{
swimming = Swimmable.swimming;
if(swimming){
gravity = - Swimmable.forceup;
}
}
This works fine except that when I´m no longer swimming I still have no gravity…
To change that I tried:
if(!swimming){
gravity = defaultgravity;
}
But this just freezes my player right at start and I no longer can move at all. Anyone sees the problem?
I wouldn’t change the gravity on an Update without a flag, because that executes the code every frame.
You’d be better off creating a flag called something like “swimmingChanged”. Set it to true when the swimming state changes, and check if that var is true, and then change the gravity, and set that var to false right away. So that will be executed once every time the gravity changes.
Where did you put the “if(!swimming)” code? Did you replace the previous “if” you had, or did you add another one?
I put it right after the previous if like this:
if(swimming){
gravity = - Swimmable.forceup;
}
if(!swimming){
gravity = defaultgravity;
}
maybe I should use else if?
Make sure the simming variable is becoming true when you’re out of the water (you could do a Debug.Log of the value on Update() to make sure it becomes true when you leave the water).
In your OnTrigger* functions, you don’t check if the collider is actually the water, or if it’s something else. If there’s another collider in the way, that could be the problem.
Also, “gravity” is not static, it’s public. Make sure you’re not setting it to zero (or another value) in the inspector, because that will take precedence on the value you’ve set in the script.
well the swimmable script is attatched to the water, not the player… so that should eliminate the problem of player not colliding with water but sth else.
My current problem is that when I enter water that is deep, then when I´m at the edge of the water(half in half out) things work fine, but as soon as I´m fully in the water it says that im out of the water o.O
EDIT:
Woah! Problem solved! I accidentally used a cube shaped mesh collider instead of a box collider. When I changed it to box collider everything works fine