Question by
morian35 · Jun 15, 2020 at 11:07 AM ·
c#scripting problemplayerprefsscore
I need help with my script please
hi iam new with unity and c# so i am making a 2d game i use playerfap to transfer score from scene 1 to scene 2 but the problem that if the player die before he get at least 1 point the game show the last score not a 0 points .
scene1 (score script attatched to player to collect score)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
private int score = 1;
private GameplayManager gameplayManager;
private void Awake()
{
gameplayManager = GameObject.FindObjectOfType<GameplayManager>();
}
void OnTriggerEnter2D(Collider2D collision)
{
gameplayManager.UpdateScore(score);
}
}
scene1 (the score come from score script to gameplaymanager script )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameplayManager : MonoBehaviour
{
public Text scoreText;
private int currentScore;
public void UpdateScore(int score)
{
currentScore += score;
scoreText.text = "" + currentScore.ToString();
PlayerPrefs.SetInt("Points", currentScore);
}
}
scene 2 (point script i use it to get the playerprefs and to us it as text for score)
public class points : MonoBehaviour
{
private int currentScore;
public Text scoreText;
// Start is called before the first frame update
void Awake()
{
currentScore = PlayerPrefs.GetInt("Points");
scoreText.text = "" + currentScore.ToString();
}
}
Comment
Best Answer
Answer by AntGoober · Jun 15, 2020 at 12:15 PM
Have you tried setting the currentScore to 0 when you first start the game?
Gameplay$$anonymous$$anager
Start() { PlayerPrefs.SetInt("Points", 0); }
That should overwrite the score that was saved in PlayerPrefs...