Hi im trying to save my players score to a file but it dosent seem to create it any ideas? it should make it in the assets folder
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
//You must include these namespaces
//to use BinaryFormatter
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameLogics : MonoBehaviour {
public GameObject Tank1;
public GameObject Tank2;
public GameObject Tank3;
public int Tank1Score;
public int Tank2Score;
public int Tank3Score;
public GUISkin CustomSkin;
float native_width = 800;
float native_height = 600;
void Start()
{
Tank1Score = PlayerPrefs.GetInt("Blues Score");
Tank2Score = PlayerPrefs.GetInt("Greens Score");
Tank3Score = PlayerPrefs.GetInt("Reds Score");
Debug.Log (Application.dataPath);
//If not blank then load it
if(File.Exists(Application.dataPath + "/highscores.dat"))
{
//Binary formatter for loading back
var b = new BinaryFormatter();
//Get the file
var f = File.Open(Application.dataPath + "/highscores.dat", FileMode.Open);
//Load back the scores
Tank1Score = (int)b.Deserialize(f);
f.Close();
}
Invoke("CountTanks", 2);
}
void CountTanks()
{
Tank1 = GameObject.Find ("Tank1(Clone)");
Tank2 = GameObject.Find ("Tank2(Clone)");
Tank3 = GameObject.Find ("Tank3(Clone)");
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.Backspace))
{
Application.LoadLevel(1);
}
if (Tank1.active == true && Tank2.active == false && Tank3.active == false)
{
Tank1Score = Tank1Score + 1;
PlayerPrefs.SetInt("Blues Score", Tank1Score);
Application.LoadLevel(1);
}
if (Tank2.active == true && Tank1.active == false && Tank3.active == false)
{
Tank2Score = Tank2Score + 1;
PlayerPrefs.SetInt("Greens Score", Tank2Score);
Application.LoadLevel(1);
}
if (Tank3.active == true && Tank2.active == false && Tank1.active == false)
{
Tank3Score = Tank3Score + 1;
PlayerPrefs.SetInt("Reds Score", Tank3Score);
Application.LoadLevel(1);
}
if (Tank3.active == false && Tank2.active == false && Tank1.active == false)
{
Application.LoadLevel(1);
}
}
void OnGUI() {
GUI.skin = CustomSkin;
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
GUILayout.BeginArea (new Rect (250,30,400,30));
GUILayout.BeginHorizontal();
GUILayout.Button("Blue: "+Tank1Score.ToString());
GUILayout.Button("Green: "+Tank2Score.ToString());
GUILayout.Button("Red: "+Tank3Score.ToString());
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
GUILayout.BeginArea (new Rect (250,60,400,30));
GUILayout.BeginHorizontal();
if (GUILayout.Button("Reset Score"))
{
Debug.Log("ScoresReset");
PlayerPrefs.SetInt("Greens Score", 0);
PlayerPrefs.SetInt("Blues Score", 0);
PlayerPrefs.SetInt("Reds Score", 0);
Tank1Score = PlayerPrefs.GetInt("Blues Score");
Tank2Score = PlayerPrefs.GetInt("Greens Score");
Tank3Score = PlayerPrefs.GetInt("Reds Score");
}
if (GUILayout.Button("Save Score"))
{
SaveTimes();
}
if (GUILayout.Button("Load Score"))
{
}
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
}
void SaveTimes()
{
Debug.Log("Score Saved");
//Get a binary formatter
var b = new BinaryFormatter();
//Create a file
var f = File.Create(Application.dataPath + "/highscores.dat");
//Save the scores
b.Serialize(f, Tank1Score);
f.Close();
}
}