我试图在关卡结束时保存得分,但是我遇到了一些错误。
这是得分脚本:
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个错误:第11行:无法将ScoreManager隐式转换为UnityEngine.Gameobject,第25行:非静态字段,方法或属性需要对象引用。
我也添加了保存/加载脚本,只是有人需要信息或者可以使用脚本:
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;
}
答案 0 :(得分:1)
第一个很简单,你在这一行中将你的变量声明为GameObject:
GameObject Score;
当你真的想要存储ScoreManager时。您需要将其更改为:
ScoreManager Score;
第二个是通过改变
来解决的int newScore = (int)ScoreManager.score; //get score and put in newScore as int
到
int newScore = (int)Score.score; //get score and put in newScore as int
因为“ScoreManager”是类的名称,而您使用的实例名为“Score”。也许看一下静态函数是什么;)我还建议你将Score变量重命名为明确表示它实际上是ScoreManager的东西。我通常只使用
ScoreManager scoreManager;
或
ScoreManager myScoreManager;
请注意实例名称通常以小写字符开头,以及具有大写字符的类。这就是为什么在代码“Score”中突出显示,stackoverflow认为它是一个类,当它实际上是一个实例名称