0
我想保存在關卡結束了比分,並且我得到了一些錯誤。保存得分從其他腳本savescript
這裏是得分腳本:
using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour {
public float score;
private IEnumerator Wait() {
yield return new WaitForSeconds(3);
Application.LoadLevel(Application.loadedLevel);
}
void TimerOfDeath(){
if(score <= 0){
GameObject.Find("TooLateGUI").guiTexture.enabled = true;
GameObject.Find("Score").guiText.enabled = false;
StartCoroutine(Wait());
}
}
void Update() {
{
score -= 60 * Time.deltaTime;
guiText.text = "Score: " + (int) score;
TimerOfDeath();
}
}
}
,並在級別結束保存腳本:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Saving : MonoBehaviour
{
GameObject Score;
void Start(){
Score = GameObject.Find("Score").GetComponent<ScoreManager>();
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
GameObject[] NoOrbs;
NoOrbs = GameObject.FindGameObjectsWithTag("Pickup");
int count = NoOrbs.Length;
if(count == 0){
GameControl.control.levelcount += 1; //add +1 to levelcount
int newScore = (int)ScoreManager.score; //get score and put in newScore as int
GameControl.control.score = newScore; //score from GameControl = this new score
GameControl.control.Save();
}
}
}
}
2個錯誤:line11:不能隱式轉換ScoreManager到UnityEngine.Gameobject,行25:對象引用需要非靜態字段,方法或屬性..
i'l添加保存/加載腳本也只是櫃面有人會需要的信息或可以使用腳本:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameControl : MonoBehaviour {
public static GameControl control;
public float score;
public int levelcount;
void Awake() {
if(control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if(control != this)
{
Destroy(gameObject);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.score = score;
data.levelcount = levelcount;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
score = data.score;
levelcount = data.levelcount;
}
}
public void overwriteSaveFile()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
File.Delete(Application.persistentDataPath + "/playerInfo.dat");
}
}
}
[Serializable]
class PlayerData
{
public float score;
public int levelcount;
}
感謝隊友,現在沒有解決構建錯誤,我仍然得找出如何i'l添加分數到一個數組和顯示各級最高分,但我現在已經得到了進一步的比我想我會再次感謝 – 2014-09-19 00:43:21
NP,只是嘗試一下,直到你不能再得到,然後再回到這裏:) – Tom 2014-09-19 01:32:50
您好再次湯姆,我已經得到了進一步有點自從昨天以來,但我不確定我是否正確地做,以及如何填補最後的漏洞。 我問過關於這個問題的一個新的問題,你會介意坐一下,好嗎? http://stackoverflow.com/questions/25938533/highscore-display-system-for-multiple-levels – 2014-09-19 22:24:43