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