ゲーム画面で記録したスコアを結果画面(ゲーム画面とは別のシーン)に表示させ、ハイスコアも表示させたいのですが、エラーが出ていて困っています。
ご教授願います。
↓結果画面のスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class ResultScoreController : MonoBehaviour { 7 public BottomwallController playerScript; 8 public Text scoreText; 9 public Text highScoreText; //ハイスコアを表示するText 10 private int highScore; //ハイスコア用変数 11 private string key = "HIGH SCORE"; //ハイスコアの保存先キー 12 13 void Start () { 14 int Rscore; 15 Rscore = playerScript.score; 16 scoreText.text = Rscore.ToString (); 17 if (Rscore > highScore) { 18 highScore = Rscore; 19 //ハイスコア更新 20 PlayerPrefs.SetInt(key, highScore); 21 PlayerPrefs.Save (); 22 Debug.Log ("Save"); 23 //ハイスコアを保存 24 highScoreText.text = highScore.ToString(); 25 //ハイスコアを表示 26 } 27 } 28} 29
↓ゲーム画面のスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class BottomwallController : MonoBehaviour { 7 public Text scoreText; 8 public int score = 0; 9 10 public int GetBulletCount(){ 11 return score; 12 } 13 14 void Start() { 15 score = 0; 16 scoreText.text = "0"; 17 } 18} 19
↓エラー内容
NullReferenceException: Object reference not set to an instance of an object
ResultScoreController.Start () (at Assets/Scripts/ResultScene/ResultScoreController.cs:15)
追記
Unity シーン間で変数を共有する方法
こちらを参考に、スクリプトを変更しました。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class BottomwallController : MonoBehaviour { 7 public Text scoreText; 8 public static int score = 0; 9 10 public int GetScore(){ 11 return score; 12 } 13 14 void Start() { 15 score = 0; 16 scoreText.text = "0"; 17 } 18 19 void OnCollisionEnter2D(Collision2D other) { 20 string layerName = LayerMask.LayerToName(other.gameObject.layer); 21 if(other.gameObject.tag == "yasai") { 22 if (layerName == "heavy") { 23 score += 7; 24 scoreText.text = score.ToString (); 25 Debug.Log ("更新"); 26 } 27 if (layerName == "usually") { 28 score += 5; 29 scoreText.text = score.ToString (); 30 Debug.Log ("更新"); 31 } 32 if (layerName == "light") { 33 score += 3; 34 scoreText.text = score.ToString (); 35 Debug.Log ("更新"); 36 } 37 } 38 if (other.gameObject.tag == "enemy") { 39 if (layerName == "heavy") { 40 score += -10; 41 scoreText.text = score.ToString (); 42 Debug.Log ("更新"); 43 } 44 if (layerName == "usually") { 45 score += -7; 46 scoreText.text = score.ToString (); 47 Debug.Log ("更新"); 48 } 49 if (layerName == "light") { 50 score += -5; 51 scoreText.text = score.ToString (); 52 Debug.Log ("更新"); 53 } 54 } 55 } 56} 57
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class ResultScoreController : MonoBehaviour { 7 public BottomwallController playerScript; 8 public Text RscoreText; 9 public Text highScoreText; //ハイスコアを表示するText 10 private int highScore; //ハイスコア用変数 11 private string key = "HIGH SCORE"; //ハイスコアの保存先キー 12 13 void Start () { 14 int Rscore = BottomwallController.GetScore (); 15 Rscore = playerScript.GetScore; 16 RscoreText.text = Rscore.ToString (); 17 if (Rscore > highScore) { 18 highScore = Rscore; 19 //ハイスコア更新 20 PlayerPrefs.SetInt(key, highScore); 21 PlayerPrefs.Save (); 22 Debug.Log ("Save"); 23 //ハイスコアを保存 24 highScoreText.text = highScore.ToString(); 25 //ハイスコアを表示 26 } 27 } 28}
変更したところエラーが2つ出ていて困っています。
ご教授願います。
Assets/Scripts/ResultScene/ResultScoreController.cs(14,37): error CS0120: An object reference is required to access non-static member `BottomwallController.GetScore()'
エラー1
Assets/Scripts/ResultScene/ResultScoreController.cs(15,25): error CS0428: Cannot convert method group
GetScore' to non-delegate type
int'. Consider using parentheses to invoke the method
エラー2
追記2
今の状況
・BottomwallControllerはゲーム画面中のスコアを表示するTextにアタッチしている
・ResultScoreControllerは結果画面の**スコア(ハイスコアではない)**にアタッチしている
↓ゲーム画面のスコアTextにアタッチしているスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class BottomwallController : MonoBehaviour { 7 public Text scoreText; 8 public static int score = 0; 9 10 public static int GetScore(){ 11 return score; 12 } 13 14 void Start() { 15 score = 0; 16 scoreText.text = "0"; 17 } 18}
↓結果画面のスコアTextにアタッチしているスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class ResultScoreController : MonoBehaviour { 7 public BottomwallController playerScript; 8 public Text scoreText; 9 public Text highScoreText; //ハイスコアを表示するText 10 private int highScore; //ハイスコア用変数 11 private string key = "HIGH SCORE"; //ハイスコアの保存先キー 12 13 void Start () { 14 int Rscore; 15// Rscore = playerScript.GetScore; 16 scoreText.text = BottomwallController.score.ToString (); 17 highScore = PlayerPrefs.GetInt(key, 0); 18 //保存しておいたハイスコアをキーで呼び出し取得し保存されていなければ0になる 19 highScoreText.text = "HighScore:" + highScore.ToString(); 20 //ハイスコアを表示 21 PlayerPrefs.GetInt(key, 0); 22 if (Rscore > highScore) { 23 24 highScore = Rscore; 25 //ハイスコア更新 26 27 PlayerPrefs.SetInt(key, highScore); 28 PlayerPrefs.Save (); 29 //ハイスコアを保存 30 31 highScoreText.text = highScore.ToString(); 32 //ハイスコアを表示 33 } 34 } 35}
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/30 14:28
2018/07/30 14:38 編集