Can't get static variable to go up

I’m having an issue where I’m trying to get a static variable to rise whenever certain other variables reach a certain point. To be specific, I’m writing code for a boss monster in a 2D shooter. The monster is comprised of 3 cubes and when a cube takes 3 shots from the player, it becomes damaged. When all five of these cubes take 3 hits each, the boss is to self-destruct. Right now, I’ve got a static variable called bosscount, inside the BossCockpitScript, set up to keep track of how many cubes have been damaged. I have another variable called hitcount that’s specific to each cube and keeps track of how many hits the cube has taken. Here’s some sample code that I put into the function Update function.

//if the hitcount ever goes above 3…

if (hitcount > 3) {

//the hitcount resets to 3

hitcount = 3;


if (bosscount == 5) {

//create explosion
var tempExplosion: Transform;

tempExplosion = Instantiate(explosion, transform.position, transform.rotation);

//destory ship piece.




Beyond that, there’s the OnTriggerEnter function into which I put the section of the code dealing with advancing the individual hit count.

  function OnTriggerEnter(otherObject : Collider){

//if a bullet hits the enemy…

if(otherObject.gameObject.tag == “bullet”) {

//increase hitcount

hitcount += 1;


//if enemy has taken three hits

if (hitcount == 3) {

//change the material to the damage set

cube.renderer.material = mat02;

bosscount += 1;



In other functions, I have specified BossCockpitScript.bosscount += 1; to access the statif variable. Yet, even when the cube containing this static variable is hit, it doesn’t rise in number. Any ideas?

It’s a little unclear what your script setup is like, but here’s how I would do it: have a script attached to each of the five cubes on the boss (say it were called BossHitArea). In that script’s OnTriggerEnter, you can increment a non-static variable called hitCount.

Then, when hitCount == 3, you can call BossCockpitScript.bossCount += 1.

From there, you can use the update function in BossCockpitScript or some other method to check when bossCount is equal to five.

It might also be worthwhile to ensure that your trigger function is even being called when shot. There are certain combinations of colliders/rigidbodies/kinematic rigidbodies that do not trigger events as you might think. Check out for more about that.

adding to what kromenak said:

You should try to debug your script. Open the project in MonoDevelop and use the “attach to process” option in the run menu to attach the debugger to the running Unity process. Put a breakpoint on the function OnTriggerEnter (by clicking left to the line number) and the hit play in the Unity editor. As soon as the breakpoint is triggered the debugger will halt the execution of the game and you can step through the code that is executed.

It may be that the function is never actually triggered and then the problem is not what you thought it was in the first place.

Another way to help you debug a program is to make use of Debug.Log(“some debug message”);
As allways Google is your friend.

I’ve got a setup similar to that. Each of the boss cubes (each cube has it’s “is trigger” box checked) does have a function that keeps track of how many times it’s been shot incremented in the OnTriggerEnter function. I had the script on each cube set to increment the bossCount script += 1 once the hitCount advances to 3. It was set as an if condition. It looked sort of like this…

if(hitCount == 3) {

bossCount += 1


Now, the check for how many times an object is it appears to be working fine. Each time the player’s bullet strikes one of the cubes, that variable goes up. I have set the textures on each cube to change after the cube is hit three times by a shot. So far, the textures change just fine when the hitCount == 3. So I know that the hitCount is advancing correctly within each cube. What’s not moving, though, is the bossCount variable.

I can try to paste the entire code here, but it’s got a lot of extra junk in it for handling the cube’s left and right movement and firing shots at the player. If that’d help more, though, I can try to post that up here (although I’m having a hard time getting things in the proper space format - even when I click the preformated text button at the top of the window, everything comes out muddled).

I’m wondering, though, does the bossCount variable need to have a function to it’s own in order to advance properly? Or could I keep it in the BossCockpitScript, since that script also has a hitCount variable.

Okay, did some moving around on my own and I figured out how to solve it. For one reason or another, Unity didn’t like static function bossCount being in the same script that had the normal hitCount variable. By making a separate script just for the static bossCount and then having each script attached to each cube advance the static variable in that script, it managed to get that number to increment. After that, it was just a matter of making sure the increment checks were under OnTriggerEnter instead of update (had a brief problem where the whole boss would explode pre-emptively, since it kept incrementing on Update) a boolean for each code to handle the piece’s self-destruction and BOOM! Everything works now. Thanks for you help, guys!