2016-10-13 237 views
-1

我已將此腳本附加到遊戲對象,然後附加到按鈕腳本,但是該按鈕腳本未顯示「新遊戲」功能(點擊圖片鏈接)image。代碼似乎沒有錯誤。可能是什麼問題呢?新按鈕不起作用

using UnityEngine; 
using System.Collections; 
using System; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO; 
using UnityEngine.SceneManagement; 

public class LoadAndNew : MonoBehaviour { 
    public string LoadCode; 
    public static string GlobalLoad; 
    public string fileName = "SaveGame.dat"; 

    void Load() 

    { 

     if(File.Exists(Application.persistentDataPath + "/SaveGame.dat" )) 
     { 
      BinaryFormatter bf = new BinaryFormatter(); 
      FileStream file = File.Open(Application.persistentDataPath + "/SaveGame.dat", FileMode.Open); 
      bf.Deserialize(file); 
      file.Close(); 

     } 
    } 

    void QuitGame() 
    { 
     Application.Quit(); 
    } 
    void NewGame() 
    { 
     SceneManager.LoadScene(1); 
    } 



} 

回答

0

你缺少public關鍵字,試試這個

public void NewGame() 
{ 
    SceneManager.LoadScene(1); 
} 

你將極有可能與QuitGameLoad功能

+0

謝謝!欣賞它! –

1

對於一個功能相同的問題在按鈕露面編輯器中的OnClick事件槽,

  • 它需要是m公開。
  • 它不能是一個靜態函數。
  • 它必須 只有一個參數或沒有。

您錯過了我提到的第一個。

要刪除這些限制,您可以改爲從代碼註冊該功能。無論它是private還是public函數,它都可以工作。

public Button button1; 
void Start() 
{ 
    button1.onClick.AddListener(() => NewGame()); 
} 

void NewGame() 
{ 
    SceneManager.LoadScene(1); 
} 
+0

當然是一個更完整的答案 –