- Home /
My static variable changes, but other scripts don't notice.
So I have two scripts. One is on a character, and when it "dies," I set a static variable "Dead" to true. This script is fine and everything works like it is supposed to.
function OnTriggerEnter (hit : Collider){
if((hit.gameObject.tag == "Spikes")&&(GoingToDie == false)){
GoingToDie = true;
var WinstonExplosion2 = Instantiate(WinstonExplosionPrefab, transform.position, transform.rotation);
WinstonExplosion2.transform.parent = gameObject.transform;
yield WaitForSeconds(1);
Dead = true;
GoingToDie = false;
}
function LateUpdate (){
if(Dead){// reset on death
print("dead");
transform.position = GameObject.Find("Character_Spawn").transform.position;
//Dead = false;
// I know I'm not setting Dead back to false, but it doesn't work when I do that..
}
But I also have another script attached to a level generating actor. When "Dead" is true, it's supposed to reset its position. However, it doesn't seem to be noticing when "Dead" is true.
function Update () {
if($$anonymous$$ainControl.Dead == true){
transform.position.x = 447.5;
Elevation = 0;
print("levelgen position reset");
Start();
}
}
Can you place the "Dead" variable on a different GameObject and send a message to narrow down where the problem is?
Sorry! I haven't worked on this in a week. So I moved the "Dead" variable onto a "Test" script, and of course the console complains that "Dead" is not a part of $$anonymous$$ainControl wherever I had the variable being used. HOWEVER, I get no errors from the LevelGeneration script. I suppose that's where my problem is.
On testing with the variable placed elsewhere but the code changed to work as intended, I am still having problems with LevelGeneration.
It's as if my if($$anonymous$$ainControl.Dead == true) statement just isn't running.
I wrote an answer below, but also keep in $$anonymous$$d that there is no need to evaluate a Boolean like this: if($$anonymous$$ainControl.Dead == true)
- you should use if($$anonymous$$ainControl.Dead)
.
@ytanay There's no need to correct people's style. if ($$anonymous$$ainControlDead == true)
is perfectly fine and some of us prefer it in certain situations. There are enough tricky things for people to learn and remember without trying to force our own personal choices on them!
Answer by pazzesco · Mar 01, 2013 at 05:40 PM
Static variables can be a dicey thing. I generally try to shy away from using them as much as possible, but if you must, try doing this instead:
Make the Dead static variable itself private, and change how it's accessed from as a variable to methods, like so:
//Changed to private from public. Also changed name of variable itself to catch any places within this script it's being accessed without using GetDead()/SetDead().
private static var IsDead:boolean = false;
static function GetDead():boolean {
return IsDead;
}
static function SetDead(newDead:boolean) {
if (newDead == true) {
Debug.Log("Dead being set to true...");
} else {
Debug.Log("Dead being set to false...");
}
IsDead = newDead;
}
Doing it this way could provide a little more insight on where you could be accessing this, and may illuminate any logical errors within your code. You'll have to change code to reflect this new way, so:
if(MainControl.Dead == true)
// ...becomes...
if(MainControl.GetDead() == true)
Hope this helps.
Answer by ytanay · Mar 01, 2013 at 05:45 PM
I think your first problem is caused by the way Update methods are chained. There is no guarantee that LateUpdate on one script will run after Update on a different script.
In any case, this seems to be a weird way to accomplish what you are trying to do. What you should probably do is use GameObject.SendMessage
(over here). Move the entire code under the logic in Update to a ResetPosition()
method and call it with SendMessage. This will make sure that it will always get called, once. Never try to evaluate static variables that can change every frame - it's a recipe for disaster. In fact, I would suggest entirely avoiding static variables and methods in this case.
This will also solve a potential future problem, which is that it will get stuck on x = 447.5
.
Let me know if this works (and especially if it doesn't)
Actually the only purpose of LateUpdate is that you can be sure that all Update functions are called before any LateUpdate is called. LateUpdate
However the rest in your answer is actually right ;)
Hmmm, that's interesting - I recall reading something else regarding the order, but of course this makes a lot more sense. Thanks for the info!
Answer by Dasnya · Mar 01, 2013 at 06:56 PM
Alright, I figured it out.... There was nothing wrong with the static variables. I was setting "Dead" back to false in another script before all the other scripts ran, and I hadn't noticed.. But thank you ytanay and pazzesco for the suggestions! I'll be sure to do something about all those static variables!
hey @Dasnya , it doesn't work in my case. my static variable never changes from another scrip. i get the same value even if it executes successsfully