I’m recreating Space Invaders as my first real Unity project. I decided to add powerups for the fun of it. I added code that looks like this:
function Update()
{
if powerup == true //(it's been active for enough time)
{
powerup = false;
}
}
static function ActivatePowerup()
{
powerup = true;
}
For some reason, when I call ActivatePowerup() from other scripts, the powerup is activated and deactivated a bunch of times in a second and then ignored. Is there something about the way Update() functions that I’m missing? I’m using DateTime objects to find out how long it’s been since the powerup was activated, if that means anything.
Can you give us the rest of your function?
Update gets called every frame, so if you Change powerup to false and somewhere else to true, you will end up in a loop…
I feel like there’s something else in the code that we’re missing.
The first thing I notice is that you’re using static methods and variables. Do you have this script on a lot of objects? If so, all the objects share the same powerup variable.
The first object to run Update() should set powerup to false for all of them though, so I’m thinking maybe some of the other objects are setting it back to true?
It looks to me like your code should be running an error no?
if somethingHere ///comented out here
{
DoThis();
}
That should be an error, unless this is some new funky way that I don’t know about.
if(something somethingElse){
soSomehting();
}
I’m sorry guys, there was a pesky bug in my code. All figured out now 