- Home /
Will static variables not work if negative?
I've run into a bit of a problem that nobody's even seemed to ask any similar questions to;
For the sake of simplicity I'll condense this down to just the chunks of code at fault I've got two scripts: one that detects collision & sets a static variable when it does, & a second that controls the character & changes based on the other script's static variable.
This script is called budube, it's the collision script
static var changeMenti = -1.999;
// a variable that will set the x momentum of the movement script if changeMenti is more than 0
function Update(){
if(wolfMove.menti == changeMenti){
changeMenti = 0;}
// this is just in case OnTriggerExit fails to set changeMenti back to 0
}
function OnTriggerStay2D(other : Collider2D){
if(other.name == "springo"){
if(gameObject.name == "rightside"){
changeMenti = (-0.5);
}
if(gameObject.name == "leftside"){
changeMenti = 0.5;
}
}}
// this will set changeMenti to 0.15 or -0.15 if the left collider or right collider touches the springo respectively
function OnTriggerExit2D(other : Collider2D){
if(other.name == "springo"){
if(gameObject.name == "leftside" || "rightside"){
changeMenti = 0;
}}}
This is called wolfMove, it's {the problematic part of} the movement script
static var menti = 0; // x momentum
static var menty = 0; // y momentum
function Update (){
// this sets the x momentum to be the static variable from the budube script
if(budube.changeMenti > 0){
menti = budube.changeMenti;
}
transform.position.x = transform.position.x + menti; // moves x by menti
transform.position.y = transform.position.y + menty; // move y by menty
}
The problem I'm running into is that the script runs just fine if changeMenti is 0.15, it sets back to 0 & makes the wolfMove script act like it bounced against a spring like it's supposed to. But when it sets changeMenti to -0.15, wolfMove won't even acknowledge it. I've tested all parts of both these scripts individually & found that changeMenti won't do anything to wolfMove if it's negative. It acts like the number is still 0, even if I set changeMenti manually. Does anyone have any clue what's going on? I literally have no context as to why this is happening considering I've used plenty of other static variables that work just fine with negative numbers, what's different here?
you are only setting menit if your static variable is > 0, so you're specifically ignoring if it is -0.15.
change your code to
if(budube.change$$anonymous$$enti != 0){
menti = budube.change$$anonymous$$enti;
}
Amd report back
Answer by nuonical · Jun 30, 2017 at 09:55 PM
Your Update() function doesn't allow the value to be set below 0 :
if(budube.changeMenti > 0){
menti = budube.changeMenti;
}
transform.position.x = transform.position.x + menti; // moves x by menti
transform.position.y = transform.position.y + menty; // move y by menty
You can set this if your change is not 0. Additionally, only set the position if not 0 :
if(budube.changeMenti != 0){
menti = budube.changeMenti;
transform.position.x = transform.position.x + menti; // moves x by menti
transform.position.y = transform.position.y + menty; // move y by menty
}
Lastly, I can't see any reason you would need to use static variables. This will cause unexpected behavior if you have more than one of these objects. Instead, use a local variable. See this answer for more information.
Sweet, thanks ^-^ I dunno how I overlooked that Will both static & public variables allow other scripts to reference that variable from another script?
@jamiemon static variables are not to allow you to reference variables from other scripts. That's what GetComponent is for. You should hardly ever use static variables for any objects in your game other than maybe a few "global" manager objects (a save manager or audio manager, for example).
Please see the following for the correct way to reference scripts https://unity3d.com/learn/tutorials/topics/scripting/getcomponent
Your answer
Follow this Question
Related Questions
How to make variable accessible by other scripts but not visible in editor? 1 Answer
Increasing a score value? 2 Answers
Static Variable Problem 1 Answer
Public and Static Variables 2 Answers
Is it possible to show Static Variables in the Inspector? 10 Answers