Updating Score to new scene

Hey I have this game where you collect artifacts which give you a score and when you collide into this door the game ends and switches to a WinScreen. I have 2 levels. I want the score to show up on my WinScreen’s. On the second WinScreen I want the score from level 1 added with my level 2 score. My teacher said to make a new Javascript file and have the variable static. After that I don’t know where to start because I already have a Javascript file with my score in it. Plus I could barely understand what he wanted me to do. Not sure how to scrip it out.

My Level 1 is: Final
My Level 2 is: Final1

My win screen is: WinScreen
My second is: WinScreen2
Here is my script that I have fore my code so far

var Scores : String = "Bob";


 //svar Scores : float = 0.0;
public var scoreText:GUIText;
public var score:int;
public var level:String;
public var sound4: AudioClip;
public var Metal_Impact_Hollow: AudioClip;
public var swap: AudioClip;






function Start () 
{
//intialize our variables listed

	
	//Scores = PlayerPrefs.GetFloat("Coin");
	
	score = 0;
	
	scoreText = GameObject.Find("GUI_Score").GetComponent(GUIText);
	//call update function to update text
	UpdateScoreText();

}



function UpdateScoreText()
{
	//update the GUI Text on screen
	scoreText.text = "Score: " + score;
}



function OnGUI()
{
    if (Application.loadedLevelName == "WinScreen")
        GUILayout.Label("Score: " + score);
          
        if (Application.loadedLevelName == "WinScreen2")
        GUILayout.Label("Score: " + score);
}


/*function Awake()
{

DontDestroyOnLoad(this);

}*/




function OnControllerColliderHit(c:ControllerColliderHit)
{
Debug.Log("hit");
    if (c.gameObject.name == "vase" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 100;
		UpdateScoreText();
		
		Debug.Log("vase");
		
			audio.Play();
	}
	
	
    if (c.gameObject.name == "shield" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 500;
		UpdateScoreText();
		audio.Play();
	}
	
	  if (c.gameObject.name == "foxmask2" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 900;
		UpdateScoreText();
		audio.Play();
	}
	
	
	  if (c.gameObject.name == "boots2" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 200;
		UpdateScoreText();
		audio.Play();
	}
	
	  if (c.gameObject.name == "backdoor" )
    {
      
		Application.LoadLevel("WinScreen");
		//DontDestroyOnLoad(this);
		
		UpdateScoreText();
	
	}
	
	  if (c.gameObject.name == "Cube" )
    {
      
		Application.LoadLevel("LoseScreen");
		audio.Play();
	}
	
	if (c.gameObject.name == "Cube5" )
    {
      
		Application.LoadLevel("LoseScreen2");
		audio.Play();
	}
	
	
	
	  if (c.gameObject.name == "Cube2" )
    {
      
		Application.LoadLevel("LoseScreen");
		

		audio.Play();
	}
	
	
	if (c.gameObject.name == "Cube3" )
    {
      
		Application.LoadLevel("LoseScreen2");
		

		audio.Play();
	}
	
	
		  if (c.gameObject.name == "backdoor2" )
    {
  
		Application.LoadLevel("WinScreen2");
		 //DontDestroyOnLoad(this);
		
		UpdateScoreText();
	
	}
	
	
	

	
}

Your teacher is right. Make a script and your score static.

public static var score:int;

Then just call the score via the script name. Static variables retain their values even after scene change. Just remember to reset the score if the player wants to play again.

how do I call the score via script name? GetComponent? how would I write that? Im a noob

yourscriptname.score;

Im kinda lost after I call the score what do I do? plus I get an error

ERROR: Assets/Scripts/Score_Tracking.js(3,7): BCE0020: An instance of type ‘Items’ is required to access non static member ‘score’.

public static var score: int;
public var scoreText:GUIText;
Items.score;




function UpdateScoreText()
{
	//update the GUI Text on screen
	scoreText.text = "Score: " + score;
}



function OnGUI()
{
    if (Application.loadedLevelName == "WinScreen")
        GUILayout.Label("Score: " + score);
          
        if (Application.loadedLevelName == "WinScreen2")
        GUILayout.Label("Score: " + score);
}







function OnControllerColliderHit(c:ControllerColliderHit)
{

	
	  if (c.gameObject.name == "backdoor" )
    {
      
		Application.LoadLevel("WinScreen");
		//DontDestroyOnLoad(this);
		
		UpdateScoreText();
	
	}
	
	if (c.gameObject.name == "backdoor2" )
    {
  
		Application.LoadLevel("WinScreen2");
		 //DontDestroyOnLoad(this);
		
		UpdateScoreText();
	
	}
	
	
	

	
}

You’re suppose to get your score on a different script used in the win screen…
EDITED
Sorry, looks like you’re just calling it wrongly on the right script. What you can do is that instead of using your score in that script, you use the static score in the script use in the game.

// Game script
public static var score : int;

And in your score display script in the win screen.

function OnGUI()
{
    GUILayout.Label("Score: " + yourgamescript.score);
}

I actually used a different script I just copied and pasted what I thought I needed in my new script. But since I am noob I don’t know how to script it out.

sigh I’m starting feel pity for your teacher.
You have a script used in your game and a different script for your win screen. So in your script in your game, it has a static variable score. Your separate script for your win screen does not store score in any way. Now when you come to your win screen, you need to display your score correct? Call the script that you use in your game that is storing the score using the script’s name. Then get the score and display it on your GUI or whatever inside your script that is for your win screen.

I decided for fun just to clean up his existing code :smile:

var Scores : String = "Bob";
 //svar Scores : float = 0.0;
public var scoreText:GUIText;
public var score:int;
public var level:String;
public var sound4: AudioClip;
public var Metal_Impact_Hollow: AudioClip;
public var swap: AudioClip;

function Start () {
//intialize our variables listed
    //Scores = PlayerPrefs.GetFloat("Coin");
    score = 0;
	var tempGO : GameObject = GameObject.Find("GUI_Score");
	if (tempGO == null){
		Debug.Log("Could not find the GUI_Score GameObject");
	}else{
		scoreText = tempGO.GetComponent(GUIText);
	}
    //call update function to update text
    UpdateScoreText();
}
	
function UpdateScoreText(){
    //update the GUI Text on screen
    scoreText.text = "Score: " + score;
}

function OnGUI(){
    if (Application.loadedLevelName == "WinScreen"){
        GUILayout.Label("Score: " + score);
    }
	if (Application.loadedLevelName == "WinScreen2") {
        GUILayout.Label("Score: " + score);
	}
}

function OnControllerColliderHit(c:ControllerColliderHit){
	switch(c.gameObject.name){
		case "vase":
			DestroyAndUpdate(c, 100);
			break;
		case "shield":
			DestroyAndUpdate(c, 500);
			break;
		case "foxmask":
			DestroyAndUpdate(c, 900);
			break;
		case "boots2":
			DestroyAndUpdate(c, 200);
			break;
		case "backdoor":
			Application.LoadLevel("WinScreen");
			UpdateScoreText();
			break;
		case "Cube":
                case "Cube2":
			Application.LoadLevel("LoseScreen");
			audio.Play();
			break;
		case "Cube5":
		case "Cube3":
			Application.LoadLevel("LoseScreen2");
			audio.Play();
			break;
		case "backdoor2":
			Application.LoadLevel("WinScreen2");
			UpdateScoreText();
			break;
		default:
			Debug.Log("Nothing found for: " + c.gameObject.name);
	}
}

function DestroyAndUpdate(c : GameObject, scoreValue : int){
    Destroy(c.gameObject); // destroys the thing this script bumped into
    score += scoreValue;
    UpdateScoreText();
    audio.Play();
}

Ok well this is what I have in my script for my win screen

function OnGUI()

{

    GUILayout.Label("Score: " + Items.score);

}

and in my gamescript I changed my score variable to static and it doesn’t work

So you are absolutely sure that you changed your score variable in the Items script to static? Are there any errors in the console?

Why not just save the final score for that scene in player prefs and then in the win scene, read the two player prefs, add them together and show final score?

Do statics actually co-exist between scenes?

EDIT I just remembered they do :smile:

wolfhunter yes. I changed it. In my Items script it looks like this

public static var score:int;

and no errors

So how many instances of the Items script is existing in the game scene at one time? Basically, how many objects in the game scene has items script? Preferably, if you have a static variable, only one instance of the script with it should exist. Also, if there is no error, what is the value that was printed out onto the win screen? If it’s 0, something is overriding the score value in your game or win scene.

Well I have my Items script attatched to my character so he can pick up the Items. There are many different items that he picks up using the Items script. and for the winscreen it doesn’t show any score. It doesn’t even show “Score:”

I feel like I need to add something for my WinScreen script…

You might want to check if you might’ve done something wrong with your GUI stuff in the win screen. What is your current script for both Items and the script displaying your score?

for my Items

var Scores : String = "Bob";


 //svar Scores : float = 0.0;
 public static var scoreText:GUIText;
public static var score:int;
public var level:String;
public var sound4: AudioClip;
public var Metal_Impact_Hollow: AudioClip;
public var swap: AudioClip;






function Start () 
{
//intialize our variables listed

	
	//Scores = PlayerPrefs.GetFloat("Coin");
	
	score = 0;
	
	scoreText = GameObject.Find("GUI_Score").GetComponent(GUIText);
	//call update function to update text
	UpdateScoreText();

}



function UpdateScoreText()
{
	//update the GUI Text on screen
	scoreText.text = "Score: " + score;
}






/*function Awake()
{

DontDestroyOnLoad(this);

}*/




function OnControllerColliderHit(c:ControllerColliderHit)
{
Debug.Log("hit");
    if (c.gameObject.name == "vase" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 100;
		UpdateScoreText();
		
		Debug.Log("vase");
		
			audio.Play();
	}
	
	
    if (c.gameObject.name == "shield" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 500;
		UpdateScoreText();
		audio.Play();
	}
	
	  if (c.gameObject.name == "foxmask2" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 900;
		UpdateScoreText();
		audio.Play();
	}
	
	
	  if (c.gameObject.name == "boots2" )
    {
        Destroy(c.gameObject); // destroys the thing this script bumped into
        score += 200;
		UpdateScoreText();
		audio.Play();
	}
	
	  if (c.gameObject.name == "backdoor" )
    {
      
		Application.LoadLevel("WinScreen");
		//DontDestroyOnLoad(this);
		
		UpdateScoreText();
	
	}
	
	  if (c.gameObject.name == "Cube" )
    {
      
		Application.LoadLevel("LoseScreen");
		audio.Play();
	}
	
	if (c.gameObject.name == "Cube5" )
    {
      
		Application.LoadLevel("LoseScreen2");
		audio.Play();
	}
	
	
	
	  if (c.gameObject.name == "Cube2" )
    {
      
		Application.LoadLevel("LoseScreen");
		

		audio.Play();
	}
	
	
	if (c.gameObject.name == "Cube3" )
    {
      
		Application.LoadLevel("LoseScreen2");
		

		audio.Play();
	}
	
	
		  if (c.gameObject.name == "backdoor2" )
    {
  
		Application.LoadLevel("WinScreen2");
		 //DontDestroyOnLoad(this);
		
		UpdateScoreText();
	
	}
	
	
	

	
}

for my Win

function OnGUI()

{

    GUILayout.Label("Score: " + Items.score);

}

Well, I don’t see anything wrong with it. Your score should be printing out. If your GUI isn’t displaying, try setting it to the center of the screen using GUI.Label just to be absolutely sure it’s not outside of the screen for some mysterious explainable reason.