Weird variable caching issue

Wondering if anyone had seen this odd issue that I’m getting.

Using Unity 1.5 - and I just get the problem in the Remote.

Problem is with static vars. I have a script which contains global variables, called scriptpuzzles.

In it, I’ve declared, say
static var variable1: int=0;

In another script, I will check this
if (scriptpuzzles.variable1 == 1)
{do something;}

so as part of my dev, I’ll update the variable directly in the scriptpuzzles script when I want it to run some code in the other script. I’ve never had any problem with this at all. But Unity crashed the other day, and since then, any changes I make don’t get picked up.

However, if I make a change in scriptpuzzles, then put a print (“var=”+scriptpuzzles.variable1) line just before the check, it does pick up the correct value that I’ve set!

So it seems to be doing some caching or something, but doing a print seems to force it to clear?

Anyone seen anything like this? Not the end of the world, but just wanted to check if there is some setting or something I can change…

Hard to say for certain without actually seeing your code (please do post an example), but this sounds like a synchronization issue. Setting the crash aside as probably unrelated. I think the issue is the timing of when the script that executes and changes the value of variable1 is executed relative to when the other executing scripts are executed. There is no deterministic ordering of execution among updates across scripts, so you can’t and shouldn’t count on there being one. It sounds like you have made an assumption about this that is faulty, and, through happenstance, you made some changes that changed the ordering of execution.

The best way to get to the bottom of this is add some instrumentation around your code like this:

Debug.Log("Executing foo at time: " + Time.realtimeSinceStartup);

It is important to add the time stamps because you want to know precisely when each statement was executed. If after doing this you find explicit evidence that shows that the value of variable1 is remaining unchanged after an instruction to change its value was executed, please submit this evidence.

And, by the way, using static variables for other than constants is really an error prone approach since you have no idea who may make a change to that variable, possibly inadvertently, and when. Better to use a singletone class instance and accessor methods so you can keep tabs on issues like this.

ahh… i think i know whats happening in your code. Are you loading this resource dynamically from the resources folder? If you are, the those resources are static instances. So if you have a x=1 at the beginning of your class you are not actually getting that set because the static instance is overriding the preset value. Find the resource you are going to and click on it. You would notice that the value that is “sticky” is set there.

Hi guys,

thanks for the replies.

@HanulTech
I did consider the timing of the script stuff - as I’ve come up against that before. I just thought that seeing as the declaration isn’t even in Start or Update or whatever, but the code referencing it is in Update, I assumed an Update (regardless of which object) would be executed after all scripts had had their initialization. Is that wrong?
Will try the debug log thing to check the time anyway - but will it execute when I put it beside the variable declarations, as it’s won’t even be in a function?

I do agree with your assertion that it’s something I’ve done to change the order of execution. though being able to “set” the variable in the right order just by doing a print confused me.

@monkeygod
not quite sure I follow. I have a global object that I don’t destroy between scenes, that just has a variety of scripts against it.
in one of those scripts is just a big list of variable declarations -
static var variable1: int=2;
for example.
When you say find the resource, do you mean the object the script is attached to? if I look at the script attached to this object, I can’t see the variable - if it’s set to static it doesn’t appear in the list to change (the way it would if it wasn’t static). not sure if that’s what you meant?

just checked - sure enough, as HanulTech said, the order of execution is doing the variable referencing, before the variable declaration runs (as I said, the declaration isn’t even in start or any other function), so the object with the static var script must be starting up afterwards…

cheers for the help!

Good to hear that you found the problem. A few months ago, I finally got fed up with this issue, and and re-architected my current project so that a single global initializer handles all object initialization and reference setting and that all the updates in all game objects delay execution until that sequence has been deterministically completed. Boy, it works like a charm, and I haven’t chased one of these issues since. Once I get done with the current project, I plan to document how to do this and put it up on the wiki to share.