2014-09-19 52 views
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; 
} 

回答

1

第一个是相当简单的,你声明的变量,在这条线上游戏对象:

GameObject Score; 

当你真正要存储ScoreManager。您需要将其更改为:通过改变

int newScore = (int)ScoreManager.score; //get score and put in newScore as int 

ScoreManager Score; 

第二个问题,来

int newScore = (int)Score.score; //get score and put in newScore as int 

因为“ScoreManager”是类的名称,和实例你正在使用的是名为“分数”。也许看看静态函数是什么;)我还建议你重新命名你的Score变量,使其清楚地表明它实际上是一个ScoreManager。我通常只使用

ScoreManager scoreManager; 

ScoreManager myScoreManager; 

注意如何实例名称通常以一个小写字母和类以大写字母开头。这就是为什么在代码中“分数”被突出显示,计算器认为它是一个类时,它实际上是一个实例名称

+0

感谢队友,现在没有解决构建错误,我仍然得找出如何i'l添加分数到一个数组和显示各级最高分,但我现在已经得到了进一步的比我想我会再次感谢 – 2014-09-19 00:43:21

+0

NP,只是尝试一下,直到你不能再得到,然后再回到这里:) – Tom 2014-09-19 01:32:50

+0

您好再次汤姆,我已经得到了进一步有点自从昨天以来,但我不确定我是否正确地做,以及如何填补最后的漏洞。 我问过关于这个问题的一个新的问题,你会介意坐一下,好吗? http://stackoverflow.com/questions/25938533/highscore-display-system-for-multiple-levels – 2014-09-19 22:24:43