My build runs differently than my editor testing.

My game is a simple app for Android. It has a menu screen, a game screen, and a results screen. The screens are all different scenes in Unity. In the game scene, the score displays in the top left corner of the screen. In the results scene, the same score displays. Upon restarting to the game scene, the score resets. That all happens correctly in the editor test mode, but when I build the game and play it on PC or Android, the score on the results scene is always 0. The score works fine in the game and resets between plays, but doesn’t show up on the results screen.

Here’s my code for the two screens:

Game/Player:

//Player Script

//Inspector Variables
var tagName 	 		: String;			//allow the designer to setup a tag in the inspector
var rayDistance  		: float 	= 0;	//length of the ray for our raycast
//pre-"scriptComponentScore"
static var score 		 		: int 		= 0; 	//score for our player

//post-"scriptComponentScore"
//var scoreScript: scriptComponentScore;
//    scoreScript = GetComponent.<scriptComponentScore>();
//var gameScore = scoreScript.score;


var gameTime	 		: float 	= 20;	//amount of time the game will last
var loadWaitTime 		: float		= 3.0;	//amount of time to wait before we load the next scene
//var numberOfPointsToWin : int 		= 5;  	//number of points to win game
//Private Variables

function Start()
{
	score = 0;
	InvokeRepeating("CountDown", 1.0, 1.0);	//Repeat the countdown every second
}

//Update is called every frame
function Update () 
{
	//Use mouse button to select on game objects in the scene
	if (Input.GetMouseButtonDown(0))
	{
		var hit : RaycastHit;
		var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); //get mouse position
		//casts a ray against all colliders in the scene
		if (Physics.Raycast(ray, hit, rayDistance))
		{
			if (hit.transform.tag == tagName)
			{
				//var position = Vector3(Random.Range(-6,6),Random.Range(-4,4),0); //new random position of the game object
				//hit.transform.position = position; //move the game object to the new location
				var enemyScript = hit.transform.GetComponent(scriptActorEnemy);
				enemyScript.numberOfClicks -= 1; //reduce the number each click
				//check that the object is at 0 before adding the points to the score
				if (enemyScript.numberOfClicks == 0)
				{
					score += enemyScript.enemyPoint; //add points to our overall score
				}
				
			}
			else
			{
				print ("This is not an enemy!");
			}
		}
	}
}
function CountDown()
{
	if (--gameTime == 0) //subtract from gameTime
	{
		CancelInvoke("CountDown"); //cancel the countdown
		//if(score >= numberOfPointsToWin)
		//{
			Application.LoadLevel("sceneScreenWin");
		//}
		/*else 
		{
			Application.LoadLevel("sceneScreenLose");
		}*/
	}

}
//
function OnGUI()
{
	GUI.Label(Rect(10,10,100,20),"Score: " + score);
	GUI.Label(Rect(10,25,100,35),"Time: " + gameTime);
}

Results Screen:

//Win Screen Script

var playerScript: scriptActorPlayer;
    playerScript = GetComponent(scriptActorPlayer);
var score = playerScript.score;

//post-"scriptComponentScore"
//var scoreScript: scriptComponentScore;
//    scoreScript = GetComponent.<scriptComponentScore>();
//var endScore = scoreScript.score;

//var buttonSize : float = 50;

//

function OnGUI() 
{
	GUI.Box(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25)-60,90,50), "Score: " + score);
	
	if(GUI.Button(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25),90,50), "Play Again"))
	{
		Application.LoadLevel("sceneLevel1");
	}
	
	if(GUI.Button(Rect(((Screen.width)/2)-75,(((Screen.height)/2)-25)+60,150,50), "Return to Main Menu"))
	{
		Application.LoadLevel("sceneScreenMainMenu");
	}
	
	if(GUI.Button(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25)+120,90,50), "Exit Game"))
	{
		Application.Quit();
	}
}

/*function OnGUI()
{
	GUI.Label(Rect(10,10,100,40), "YOU WIN!!"); 
	
	if(GUI.Button(Rect(10,60,100,50), "Restart Game"))
	{
		//print("Start Game");
		Application.LoadLevel("sceneLevel1");
	}
	if(GUI.Button(Rect(10,130,90,50), "Exit Game"))
	{
		print("Exit Game");
		Application.Quit();
	}
}*/

i am sharing my own made code for GUI elements you can resolve your problems with this import attached package to unity project then put script to any empty game object then go to inspector and choose what you want to create setting position and click run game then click on screen appeared button (get code) to get code of your created element then select as you need press CTRL+C to copy then you know batter how to use…

even you want to create a popup window you can create with this easily.[19938-guielementscreator.zip|19938]

I don’t see where you “hard save” the score. For example, writing it to PlayerPrefs.

It looks like during testing/editing the score is being kept, since the program is running the entire time. If you quit Unity and restart, I’d guess the score would reset to 0. In a real build, the program quits & restarts(*) so needs to permanently save the score in a file. PlayerPrefs is the simplest way to do that.

(*)Some tablets keep it running in the background, but if the stops it, the score would be lost.

I figured it out! After posting that image, I realized that I was declaring a “score” variable on the win screen as “playerScript.score.” All I needed to do was remove that variable declaration and plug in “playerScript.score” where the “score” variable was used on the win screen. Like this:

Instead of:

//Win Screen Script
 
var playerScript: scriptActorPlayer;
    playerScript = GetComponent(scriptActorPlayer);
var score = playerScript.score;
 
//
 
function OnGUI() 
{
    GUI.Box(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25)-60,90,50), "Score: " + score);

Should be:

//Win Screen Script
 
var playerScript: scriptActorPlayer;
    playerScript = GetComponent(scriptActorPlayer);
 
//
 
function OnGUI() 
{
    GUI.Box(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25)-60,90,50), "Score: " + playerScript.score);