正在将其他脚本中的分数保存到保存脚本中
本文关键字:保存 脚本 其他 | 更新日期: 2023-09-27 18:23:42
我试图在关卡结束时保存分数,但出现了一些错误。
这是分数脚本:
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;
}
第一个非常简单,您可以在以下行中将变量声明为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认为它是一个类,而它实际上是一个实例名