Trying to save and load scores from PlayerPrefs

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?

add totalPoints = PlayerPrefs.GetInt(“TotalPoints”); on start at 2nd script

@reefer If you are still having trouble with this, I can help - there’s a few things I see wrong here, but don’t want to get into it too far if you have already fixed your issue by now…

One big issue I see is where you added “BoxCol.Points = BoxCol.totalPoints;” (found in comments from first post)… you are referring to the BoxCol static value, but you also have a local instance of a BoxCol, which you named “totalPoints”. So this line should be more like totalPoints.Points = totalPoints.totalPoints… so your naming convention is a little funky or you need to remove the instance of BoxCol, and just make Point static as well.

Another issue is I don’t see where you are adding or removing points for anything… Maybe you are doing it in another script or maybe it should be in the “OnCollisionEnter” of BoxCol… something like: totalPoints -= 1000, or Points -= 1000… or += instead of -=, depending on what you are trying to do here.