- Home /
Save points from one scene to another?
I am making a 2D platformer and I want to know how I can make it so when the player collects coins, it travels to the other scene and beyond.
Here are the scripts that use the points
FIRST SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class GameMaster : MonoBehaviour
{
public int points;
public Text pointsText;
private void Update()
{
pointsText.text = points.ToString();
}
}
SECOND SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGrounded;
private int extraJump;
public int extraJumpValue;
private GameMaster gm;
private void Start()
{
extraJump = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<GameMaster>();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGrounded);
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
Flip();
else if (facingRight == true && moveInput < 0)
Flip();
}
private void Update()
{
if (isGrounded == true)
extraJump = extraJumpValue;
if (Input.GetKeyDown(KeyCode.Space) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJump == 0 && isGrounded == true)
rb.velocity = Vector2.up * jumpForce;
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= 1;
transform.localScale = Scaler;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Coin"))
{
Destroy(collision.gameObject);
gm.points += 1;
}
}
}
Answer by Kim-Nobre · May 21, 2019 at 01:07 PM
I think the simplest way to do it is by saving your points using PlayerPrefs.
This is how you save it:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Coin"))
{
Destroy(collision.gameObject);
gm.points += 1;
PlayerPrefs.SetInt("Points", gm.points);
}
}
And this is how you get the saved value:
public class GameMaster : MonoBehaviour
{
public int points;
public Text pointsText;
private void Start()
{
points = PlayerPrefs.GetInt("Points", 0);
}
private void Update()
{
pointsText.text = points.ToString();
}
}
That does work, but there is an issue. If you pick up coins that you have had before, and then you go to the same scene and pick up those coins, you still have your points saved. Is there a way to reset them?
You can set the "Points" back to zero when you quit the game, like this:
private void OnApplicationQuit()
{
PlayerPrefs.SetInt("Points", 0);
}
Or you can do what the person in the other answer said, ins$$anonymous$$d of using PlayerPrefs, make the Game$$anonymous$$aster script a Singleton, adding this:
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
This won't save the points but will keep them as long as the game is running.
Is there a way to make it so if the player dies, and respawns, the points they got from that scene resets?
Answer by tormentoarmagedoom · May 22, 2019 at 08:17 AM
Hello.
This is Unity Answers, Not Unity Code Providers...
Go read the manual and watch tuttorials about Unity Data Persistence.
good luck! Bye!
Answer by innf107 · May 21, 2019 at 01:32 PM
If you just want to save it between scenes you could also just move your GameMaster to DontDestroyOnLoad.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
PlayerPrefs to save setActive state 2 Answers
How can you save and update data between scenes and upon log in? 1 Answer
Return to a specific scene & position 0 Answers