Hello. I’m trying to save and load total points player has got from PlayersPrefs, it works fine so far until next level starts. Total points stays at 0, even though it should show 4000 points after first level if you dont fail anything.
Heres the script thats supposed to load score from PlayerPrefs:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class BoxCol : MonoBehaviour
{
public GameObject[] boxes;
public int Points;
public static int totalPoints;
void Start()
{
totalPoints = PlayerPrefs.GetInt("TotalPoints");
}
void Update()
{
boxes = GameObject.FindGameObjectsWithTag("Box");
Debug.Log("Boxes: " + boxes.Length);
Points = boxes.Length * 1000;
if (Points == 0)
{
restartCurrentScene();
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Box")
{
Debug.Log("Box hit the ground");
Destroy(col.gameObject);
}
}
void OnGUI()
{
GUI.Box(new Rect(Screen.width / 2, 30, 200, 20), "Points: " + Points);
GUI.Box(new Rect(Screen.width / 2, 60, 200, 20), "Total Points: " + totalPoints);
}
public void restartCurrentScene()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
}
And heres the script thats supposed to save that total points score to PlayerPrefs, which it is supposed to get from that script I posted first.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Finish : MonoBehaviour {
public BoxCol totalPoints;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Truck")
{
Debug.Log("Finish line");
saveScores();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
public void saveScores()
{
PlayerPrefs.SetInt("TotalPoints", BoxCol.totalPoints);
}
}
I think it has something to do with loading it from another script as I can’t put my “BoxCol.cs” script to the place on Inspector that says “BoxCol”.
So any ideas how to fix it?