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();
}
}*/