0

I am a New person to Unity. My problem is that I have a Score in my first scene which is the position of z of the player. I want it to save it to the HighScore using PlayerPrefs. I have 2 empty objects named MenuManager and GameManager and they have their own scripts but I don't know how can I share the value of the score and save it in a PlayerPrefs. Can anyone please help me? I don't know how can I assign a component if it is not in my scene. Also can you please help me to use the function of a script of a different scene. I mean how can I call a function of a game object which is in an other scene. Please help me. I need to finish my game.

The Score Script which keeps track of players position.

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Transform player;
    public Text scoreText;
    public HighScore highScore;

    [HideInInspector]public static float totalScore;

    void Update()
    {
        scoreText.text = player.position.z.ToString("0");
        totalScore = player.position.z;
    }
}

The High Score Script which is responsible for the text changing although I havent worked on the player prefs yet if anyone knows please help me in that also.

using UnityEngine;
using UnityEngine.UI;

public class HighScore : MonoBehaviour
{
    static float floatScore;
    public Text highScore;

    private void Start()
    {
        highScore.text = PlayerPrefs.GetFloat("HighScore", 0).ToString();
    }
    public void highScoreFunc()
    {  
        PlayerPrefs.SetFloat("HighScore", floatScore);
        highScore.text = floatScore.ToString();
    }

    public void resetScore()
    {
        PlayerPrefs.DeleteKey("HighScore");
    }
}

The Menu Functions Script which have the option of using the buttons.

using UnityEngine;
using UnityEngine.SceneManagement;

public class MenuFunctions : MonoBehaviour
{

    public void StartGame()
    {
        SceneManager.LoadScene(1);
    }

    public void quitGame()
    {
        Application.Quit();
    }
}

The Game Manager which have the work of changing scenes and restarting the game.

using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    bool GameEnd = true;
    public HighScore highScore;
    

    public void EndGame()
    {
        if(GameEnd)
        {
            GameEnd = false;
            Invoke("Restart", 1);
        }
    }
  
    void Restart()
    {
        SceneManager.LoadScene(sceneBuildIndex: 0);
    }
}
  • Hey! [DontDestroyOnLoad](https://stackoverflow.com/questions/32306704/how-to-pass-data-between-scenes-in-unity) is what you’re looking for. Also you might want to implement [Singleton Pattern](https://stackoverflow.com/questions/46286996/unity-singleton-pattern) in your GameManager. – salyangoz Oct 17 '20 at 12:33
  • How can I do that do you have any suggestions – Burhanuddin Nasikwala Oct 17 '20 at 13:12
  • I actually want to share the value with both of the scenes and then I want to enable a script so I can fit it in the `PlayerPref` part to store it – Burhanuddin Nasikwala Oct 17 '20 at 13:13

1 Answers1

1
class ScoreKeeper{
  public static float HighScore;
}
//where you update player score
ScoreKeeper.HighScore= 10;//Score Value
//Where you want to fetch score
Debug.Log(ScoreKeeper.HighScore);

Explanation: Just create a class (non Mono behaviour) and use static variable to hold scores which can be accessed across multiple Scene.

As far as PlayerPrefs are concerned you are better off using Binary Files

https://www.youtube.com/watch?v=XOjd_qU2Ido&t=825s

Here is a great explaination.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Bipin
  • 451
  • 4
  • 14
  • Thanks bro I actually did it. I made common score script where I created a static variable and I had this script on two game objects on different scene. So I took the variable and from a different script I took the score and then added them in player prefs – Burhanuddin Nasikwala Oct 18 '20 at 16:33
  • okay, actually you can also not add such data script to any objects. It'll still work since it uses static variables. – Bipin Oct 19 '20 at 08:37