- Home /
How to alter c# script from a script in a different scene (i.e. display score in different scene)
I want to display the score of the last game in the start menu scene, so that when a player finishes the game and is returned to the start menu scene, their score is displayed there...
I have an empty game object in the start menu that the 'ScoreManager' script is attached to, which I've put to not destroy on load - this is linked to a 3D text. I also have two scripts that deduct and add points, calling the function in the ScoreManager script to update the total score. Can anyone tell me where I've gone wrong or how to make this work please? The code is as follows:
public class ScoreManager : MonoBehaviour {
public static int score;
public TextMesh Scoretext;
//Add points to score and trigger UpdateScore
public void AddPoints(int points)
{
score += points;
UpdateScore();
}
//Update score text
public void UpdateScore()
{
Scoretext.text = "Latest \nScore:\n" + score;
}
}
public class SmCollider : MonoBehaviour {
Vector3 Origin;
public AudioClip SmHit;
public Spawner SpawnPos;
public int addScore = 10;
public ScoreManager scoremanager;
// Detect collision w/snowman, play audio, destroy snowman and add points
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Snowman")
{
Debug.Log("Hit snowman");
Origin = SpawnPos.spawnPos;
AudioSource.PlayClipAtPoint(SmHit, Origin);
Destroy(gameObject);
Destroy(collision.gameObject);
scoremanager.AddPoints(addScore);
}
}
}
public class SbCollider : MonoBehaviour {
Vector3 Origin;
public AudioClip SbHit;
public int minusScore = -5;
public ScoreManager scoremanager;
//Detect collision w/player, play audio, destroy sb and minus points
void OnCollisionEnter(Collision collision)
{
GetComponent<SteamVR_Camera>();
if (collision.gameObject.tag == "MainCamera")
{
Debug.Log("Hit by snowball");
Origin = new Vector3(0, 0, 0);
AudioSource.PlayClipAtPoint(SbHit, Origin);
Destroy(gameObject);
scoremanager.AddPoints(minusScore);
}
}
}
Answer by hexagonius · Jul 13, 2017 at 05:24 PM
AddPoints is not a class method. You should chance it to static also. This way, it can be called everywhere, in- or decreasing your score. Further, you should put a Start method into the script that calls UpdateScore (no need for public). This way, when the scene starts, the script reads the last score and changes the text to it. Right now, the problem is that AddPoints only works when an instance exists in the scene, which doesn't because it's a non destroy on load in the start menu, but the methods is called during the game.
Thanks for the quick reply :) have done as you said, however when I change it to static my 'Scoretext.text' becomes an error and tells me that 'object reference is required for non-static field, method or property 'Score$$anonymous$$anager.Scoretext'...
public class Score$$anonymous$$anager : $$anonymous$$onoBehaviour {
public static int score;
public Text$$anonymous$$esh Scoretext;
void Start()
{
AddPoints(score);
}
//Add points to score and trigger UpdateScore
public void AddPoints(int points)
{
score += points;
UpdateScore();
}
//Update score text
public static void UpdateScore()
{
Scoretext.text = "Latest \nScore:\n" + score;
}
}
Answer by UnityCoach · Jul 13, 2017 at 06:01 PM
Isn't this something you want to save from a session to the next? If so, you should use
PlayerPrefs.SetInt ("LastScore", score);
then
PlayerPrefs.GetInt ("LastScore", 0);