2017-06-02 16 views
-1

Unity(C#):別のシーンからリストを呼び出すにはどうすればよいですか?現在のシーンでnullReferenceException bcが返された場合、変数は設定されていません。しかし、前のシーンでは、私には正しい値と変数がありました。 (注:2つのシーンは異なりますが、シーン間の共有値としてスコアを使用したい)。Unity(C#):List <int>を別のシーンから呼び出すにはどうすればいいですか?

それ以外の場合は、「frameTexts [9] .text」の位置を取得する代わりに利用できますか?

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 


public class ScoreDisplay : MonoBehaviour 
{ 

    public Text[] rollTexts, frameTexts; 
    static string output; 

    public void FillRolls(List<int> rolls) 
    { 
     string scoreString = FormatRolls(rolls); 
     for (int i = 0; i < scoreString.Length; i++) 
     { 
      rollTexts[i].text = scoreString[i].ToString(); 

     } 
    } 

    public void Fillframes(List<int> frames) 
    { 
     for (int i = 0; i < frames.Count; i++) 
     { 
      frameTexts[i].text = frames[i].ToString(); 
      Debug.Log(frameTexts[i].text + " frames"); 
     } 
    } 

    public static string FormatRolls(List<int> rolls) 
    { 
     // ScoreDisplay scoreDisplay = new ScoreDisplay(); 
     output = ""; 

     for (int i = 0; i < rolls.Count; i++) 
     { 
      int box = output.Length + 1;       // Score box 1 to 21 

      if (rolls[i] == 0) 
      {         // Always enter 0 as - 
       output += "-"; 
      } 
      else if ((box % 2 == 0 || box == 21) && rolls[i - 1] + rolls[i] == 10) 
      { // SPARE anywhere 
       output += "/"; 
      } 
      else if (box >= 19 && rolls[i] == 10) 
      {    // STRIKE in frame 10 
       output += "X"; 
      } 
      else if (rolls[i] == 10) 
      {       // STRIKE in frame 1-9 
       output += "X "; 
      } 
      else 
      { 
       output += rolls[i].ToString();      // Normal 1-9 bowl 
      } 

     } 
     return output; 
    } 
    public string DisplayFinalScore() 
    { 
     string FinalScore = "Congrats on scoring: " + frameTexts[9].text; 
     return FinalScore; 
    } 

} 
+2

リストを静的にして、モノオブジェクトから継承しないクラス内にする – Lestat

+2

[シーン間のユニティパスデータ](https://stackoverflow.com/questions/32306704/unity-pass-data)の可能な複製-between-scenes) – Serlite

答えて

1

あなたのリクエストは、あなたのプロジェクトで何をしたいのかによって異なります。 しかし、私はいくつかのいくつかのソリューションをお勧めすることができます:

  • シングルトンunitygeekからgist
  • 外部ファイルにあなたのスコアを保存PlayerPref

あなたは維持したい場合ゲーム全体の得点は、私が考えるとシングルトンが素敵になります!

2

使用すると、1つのシーンから別のものに保持する変数を含むオブジェクトのスクリプトで使用DontDestroyOnLoad

+0

DontDestroyOnLoadを "DontDestroyOnLoad(frameTexts [i]);"として使用しました。しかし、「DontDesotryOnLoadはルートGameObjectまたはルートGameObject上のコンポーネントでのみ動作します」という警告が表示されます。以前と同様にNullReferenceExceptionが発生します。それがなぜ機能しないのか何か手がかり? – BenSmith

+0

変数にDontDestroyOnLoadを使用することはできません。スクリプトを空のゲームオブジェクトにアタッチし、このDontDestroyOnLoad(transform.gameObject)を呼び出します。 transform.gameObjectは破壊しないオブジェクトです。 – rohankad

+0

** MonoBehaviour **クラスで 'DontDestroyOnLoad(gameObject)'または 'DontDestroyOnLoad(this)'を使用することができます(ユニティの例が 'DontDestroyOnLoad(transform.gameObject)'である理由は不明ですが: 。また、 'DontDestroyOnLoad'スクリプトを保持しているオブジェクトは、ルートオブジェクト(別のオブジェクトの子ではない)でなければなりません。親に' DontDestroyOnLoad'スクリプトがなければ、その子は削除されます。 – Kardux

0

ゲームオブジェクトが次のシーンでアクティブになるようにDontDestroyOnLoad(transform.gameObject);を使用するか、PlayerPrefsを使用してデータを保存し、次のシーンに読み込むことができます。どちらも動作します!

関連する問題