Hello!
Im using c#, and i have this script. Note, that this is not the full script. I deleted all others, so it's not so messy and you can read it easier ( I know, i have bad english knowledge ).
There are 2 scenes: MainMenu, and the Map.
This is the script in the Map, named PlayerControl. ( It will give you money by picking up but now i coded by pressing ''A'' key. ) When i leave ( escape button ) it goes to the menu. When i go back ( There is a button that drops the player to ''Map'' ) it doesn't saves the players ''money''.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public int money;
// Use this for initialization
void Start () {
PlayerPrefs.GetInt("money", money);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
PlayerPrefs.Save();
Application.LoadLevel("Menu");
}
if (Input.GetKeyDown(KeyCode.A)) {
money = money + 1;
PlayerPrefs.SetInt("Money", money);
}
}
void OnGUI () {
GUI.Label (new Rect (50, 75, 100, 30), "Money");
GUI.Label (new Rect (150, 75, 100, 30), money.ToString());
}
}
Thank you!
( Here is the full script, but im almost 100% sure that these are not required for saving )
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public float accelerationofallstuff = 1;
public static float nanometersinglerun = 0;
public int money;
// Use this for initialization
void Start () {
Screen.showCursor = false;
bool showrewardsondeath = false;
PlayerPrefs.GetInt("money", money);
}
// Update is called once per frame
void Update () {
accelerationofallstuff = Time.timeSinceLevelLoad / 20;
nanometersinglerun = Time.timeSinceLevelLoad * accelerationofallstuff;
bool showrewardsondeath = false;
float deltax = Input.GetAxis("Mouse X");
transform.position = new Vector3 (transform.position.x + deltax, 0, 0);
if (transform.position.x > 10)
transform.position = new Vector3 (10,0,0);
if (transform.position.x < -10)
transform.position = new Vector3 (-10,0,0);
if (Input.GetKeyDown(KeyCode.Escape)) {
PlayerPrefs.Save();
Application.LoadLevel("Menu");
}
if (Input.GetKeyDown(KeyCode.A)) {
money = money + 1;
PlayerPrefs.SetInt("Money", money);
}
}
void OnGUI () {
GUI.Label (new Rect (50, 25, 100, 30), "Nanometer");
GUI.Label (new Rect (50, 50, 100, 30), "Time");
GUI.Label (new Rect (50, 75, 100, 30), "Money");
GUI.Label (new Rect (150, 25, 100, 30), nanometersinglerun.ToString("F1"));
GUI.Label (new Rect (150, 50, 100, 30), Time.timeSinceLevelLoad.ToString("F1"));
GUI.Label (new Rect (150, 75, 100, 30), money.ToString());
if (Time.timeSinceLevelLoad < 2)
GUI.Label (new Rect (Screen.width /2 -50, 50, 160, 30), "Activating Nanobot");
if (Time.timeSinceLevelLoad > 3)
GUI.Label (new Rect (Screen.width /2 -30, 50, 100, 30), "Charging...");
}
}
↧