2014-09-19 32 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