Why isn't this function working??

i have a script that runs each time I reload the scene with

Application.loadLevel(Application.loadedLevel)

(i.e. each time players play one “round” in a best of x match) to reinitialize the UI
I have 3 functions that are called. One updates the Wall power cooldown text, one updates the Bomb power cooldown text, and one updates the number of round wins for each player. All 3 functions are exactly the same:

//called in roundWins to update every player's UI
public void updateWinsText(string changeToThis)
{
	winsText.GetComponent<TextMesh>().text = changeToThis;
}
	
//called in Powers.cs when dealing with the cooldown
public void updateBombTimerText(string changeToThis)
{
	bombTimerText.GetComponent<TextMesh>().text = changeToThis;
}
	
//called in Powers.cs when dealing with the cooldown
public void updateWallTimerText(string changeToThis)
{
	wallTimerText.GetComponent<TextMesh>().text = changeToThis;
}

Whenever my initialization script runs, it calls all 3, and on the first round (i.e. right after I hit “play”) all 3 work fine. But when I reload the scene, it CALLS all 3, but the update wins function doesn’t set the UI element to the new amount of wins for the player that won the last round. I’ve been trying to figure this out for hours and I just dont get it

  1. I know for a fact it calls the function (tested with debug.log)
  2. I know for a fact it’s setting it to the correctly updated value (tested with debug.log)
  3. I know for a fact there are no “missing elements” in my gameobjects’ scripts (double checked, and I never destroy anything – only set active and deactive)
  4. I call the initialization AFTER reloading the new scene, so it’s not getting overwritten
  5. I know for a fact it sets it correctly on start

So… wtf is going on…?

These are always “fun” to debug. I usually try to narrow things down by adding code to change the value in other parts of the program. In this case I would start by setting winsText to a hard coded value using code that runs completely independently of the load level - controlled by either a key or some other event that happens during the game.

If that doesn’t work then it would indicate that something is messed up with the object for some reason. If the value does change then start working your way back through the level loading code, hard coding values until it breaks. Sometimes I’ll add a ton of them, with values like “1”, “2”, “3”, etc. that let me see where things stop working.

Are you absolutely sure you’re not setting the value until after the level is completely loaded? Is it possible that Unity is still serializing the saved values after you’re setting your value? You could test this by using Invoke with a delay in your function so the value isn’t set until 1 or more seconds later. I’ve had luck finding problems like this using that method when I suspected it was some sort of timing problem.

You could also use the Mono debugger to trace through things.

Sounds like you may have done these things, but without more code it will be difficult to suggest anything beyond what you’ve already tried.