So i made a score system which is dependent on the position of the player on the z axis. But i can’t figure out how I can keep the score in the next scene or level.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Score : MonoBehaviour
{
public Transform player;
public Text score;
public float iniPos;
void Start()
{
iniPos = player.position.z;
}
// Update is called once per frame
void Update()
{
score.text = ((player.position.z - iniPos).ToString(“0”));
}
}
1 Like
see Using code tags properly
I never did this, but unity persisting data at DuckDuckGo finds many guides/tutorials (“saving” = “persisting”)
unity saving data at DuckDuckGo also has plenty of hits
1 Like
As was mentioned above, you need to persist some data, which is your score, to the next level. There are different approaches for this. Static variables dont get reset when the scene changes, PlayerPrefs can be used to store and load data from, your could save the data to a temporary file, or make the Score gameobject persist between scenes using DontDestroyOnLoad(). Whatever you chose, you shoul start by actually saving your score at all. Currently no ‘score’ exists. You are simply printing the current position minus some initial position to the screen.
If i understoof correctly, you create some sort of runner game, where the distance travelled equals the score achieved. Eventually you complete a level and enter a new one, which is done by changing scenes. You want the score from the previous scene to carry over to the new one unless the player dies. If this is correct, i would go with the following:
Make the Score gameobject persist between scenes using DontDestroyOnLoad(). Add a ‘List oldScores’ to it. When you complete a level, before changing scenes add the score to the list by writing oldScores.Add(currentScore). Your total score at any point is the sum of all entries in oldScores plus the current score. When the player dies or you need to reset the score for some other reason (ie entering the main menu, …), simply Destroy() the score object. Optionally save the score to PlayerPrefs then, which also persists between game sessions. You can use this to compare the achieved score to the previous highScore and set a new highScore, should the value be higher.
Thanks guys. So i used PlayerPrefs to preserve the data and all the code is going well but the prob is even though i’m calling the function to erase the playerpref it isn’t working.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Score : MonoBehaviour
{
public Transform player;
public Text score;
public float iniPos;
public float OldScore;
public float NewScore;
void Start()
{
iniPos = player.position.z;
OldScore = PlayerPrefs.GetFloat("sc", 0);
}
// Update is called once per frame
void Update()
{
NewScore = OldScore+(player.position.z - iniPos);
score.text = NewScore.ToString("0");
PlayerPrefs.SetFloat("sc", NewScore);
}
public void Reset()
{
PlayerPrefs.DeleteKey("sc");
}
}
So this is like my Score script and I got another Game Manager for managing different game states etc etc
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GM : MonoBehaviour
{
public bool gamehasended = false;
public GameObject canvas;
public GameObject gameover;
public AudioSource main;
public Score marks;
public bool nextlvl = false;
public void completeLevel()
{
canvas.SetActive(true);
nextlvl = true;
}
public void EndGame()
{
if (gamehasended == false)
{
gamehasended = true;
if (nextlvl == true)
{
gameover.SetActive(false);
}
else
gameover.SetActive(true);
marks.Reset();
main.Stop();
Invoke("restart", 4f);
}
}
public void restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
The prob is Marks.Reset is not working. Any suggestions?
Please use code tags to post code.
Update: I figured it out. U just have to place the DeleteKey function in the update() and run an if statement to check whether the gamehasend or not.