現状
ボタンでスコアモードとタイムモードに切り替えるようにしてそれぞれのシーンに移動するようにした
ゲームジェネレーターでゲーム進行管理している
タイムのほうはタイムゲームジェネレーターに名前変更した(ほかのオブジェクトはすべてコピペ)
始めるとスコアのほうは正常にできるがタイムのほうにするとエラーが出る
NullReferenceException: Object reference not set to an instance of an object
TimeGameManager.Start () (at Assets/Scripts/TimeGameManager.cs:38)
NullReferenceException: Object reference not set to an instance of an object
TimeGameManager.LateUpdate () (at Assets/Scripts/TimeGameManager.cs:53)
実現したいこと
タイムモードでもゲームオーバーになってほしいのとタイムが減らない
該当コード
```unity scoreコード public class GameManager : MonoBehaviour { //ゲームステート enum State { Ready, Play, GameOver } State state; int score;//スコア int coin;//コイン public Text StateLabel; public Text scoreLabel; public Text coinLabel; public Text endscoreLabel; public Text endcoinLabel; public GameObject EnemyGenerator;//敵生成オブジェ public GameObject CoinGenerator;//Coin生成オブジェ PlayerCotroller playcon; GameObject Player; public GameObject Title;//タイトルボタン public GameObject Retry;//タイトルボタン private float count; // Start is called before the first frame update void Start() { //開始時にRedayステート開始 Ready(); Player = GameObject.FindWithTag("Playercon"); playcon = Player.GetComponent<PlayerCotroller>(); Time.timeScale = 1; PlayerPrefs.SetInt("NowScore", 0); } void LateUpdate() { //ステートごとにイベント監視 switch (state) { case State.Ready://タッチしたらゲームスタート if (Input.GetButtonDown("Fire1")||Input.GetKeyDown("space")) GameStart(); break; case State.Play://車が破壊されたらゲームオーバー if (playcon.IsDead()) GameOver(); break; case State.GameOver: count += Time.deltaTime; if (count >= 1.0f) { Time.timeScale = 0; Title.gameObject.SetActive(true);//タイトルボタン表示 StateLabel.gameObject.SetActive(false); scoreLabel.gameObject.SetActive(false); coinLabel.gameObject.SetActive(false); if (Input.GetKeyDown("space")) { //ゲームシーンを読み込み SceneManager.LoadScene("Game"); } if (Input.GetKeyDown("right")||Input.GetKeyDown("left")) { //タイトルシーンを読み込み SceneManager.LoadScene("Title"); } } break; } } // Update is called once per frame void Update() { } void Ready() { state = State.Ready; //オブジェクトを無効にする EnemyGenerator.SetActive(false); CoinGenerator.SetActive(false); //ラベル更新 scoreLabel.text = "Score : " + 0; StateLabel.gameObject.SetActive(true); StateLabel.text = "READY"; coinLabel.text = "Coin : " + 0; } void GameStart() { state = State.Play; //オブジェクトを有効にする EnemyGenerator.SetActive(true); CoinGenerator.SetActive(true); //ラベル更新 StateLabel.gameObject.SetActive(false); StateLabel.text = ""; } void GameOver() { state = State.GameOver; //ラベル更新 StateLabel.gameObject.SetActive(true); //オブジェクトを無効にする EnemyGenerator.SetActive(false); CoinGenerator.SetActive(false); StateLabel.text = "GAMEOVER"; //ハイスコアを更新 if (PlayerPrefs.GetInt("ScoreHighScore") < score) { PlayerPrefs.SetInt("ScoreHighScore", score); } PlayerPrefs.SetInt("Totalcoin", coin + PlayerPrefs.GetInt("Totalcoin")); endscoreLabel.text = "Score : " + score; endcoinLabel.text = "Coin : " + coin; } public void IncreaseScore() { if (playcon.IsDead()) return; score += 50; //スコア更新 scoreLabel.text = "Score : " + score; PlayerPrefs.SetInt("NowScore", score); } public void IncreasePlayerScore() { if (playcon.IsDead()) return; score += 10; //スコア更新 scoreLabel.text = "Score : " + score; PlayerPrefs.SetInt("NowScore", score); } public void IncreasCoin() { if (playcon.IsDead()) return; coin++; coinLabel.text = "Coin : " + coin; } public void OnClick() { //タイトルシーンを読み込み SceneManager.LoadScene("Title"); } public void OnRetry() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } }
unity
1timeコード 2 3public class TimeGameManager : MonoBehaviour 4{ 5 //ゲームステート 6 enum State 7 { 8 Ready, Play, GameOver 9 } 10 State state; 11 int score;//スコア 12 int coin;//コイン 13 float time; 14 public Text StateLabel; 15 public Text scoreLabel; 16 public Text coinLabel; 17 public Text endscoreLabel; 18 public Text endcoinLabel; 19 public Text TimeLabel; 20 public GameObject EnemyGenerator;//敵生成オブジェ 21 public GameObject CoinGenerator;//Coin生成オブジェ 22 PlayerCotroller playcon; 23 GameObject Player; 24 public GameObject Title;//タイトルボタン 25 public GameObject Retry;//タイトルボタン 26 private float count; 27 28 // Start is called before the first frame update 29 void Start() 30 { 31 //開始時にRedayステート開始 32 Ready(); 33 Player = GameObject.FindWithTag("Playercon"); 34////////////////////////////////////////////////////////////////////// 35 //38 playcon = Player.GetComponent<PlayerCotroller>(); 36//////////////////////////////////////////////////////////////////////////// 37 Time.timeScale = 1; 38 PlayerPrefs.SetInt("NowScore", 0); 39 time = 60; 40 } 41 42 void LateUpdate() 43 { 44 //ステートごとにイベント監視 45 switch (state) 46 { 47 case State.Ready://タッチしたらゲームスタート 48 if (Input.GetButtonDown("Fire1") || Input.GetKeyDown("space")) GameStart(); 49 break; 50 case State.Play://車が破壊されたらゲームオーバー 51/////////////////////////////////////////////////////////////////////////////////// 52 //53 if (playcon.IsDead()|| time<=0) GameOver(); 53////////////////////////////////////////////////////////////////////////////////////// 54 break; 55 case State.GameOver: 56 count += Time.deltaTime; 57 if (count >= 1.0f) 58 { 59 Time.timeScale = 0; 60 Title.gameObject.SetActive(true);//タイトルボタン表示 61 StateLabel.gameObject.SetActive(false); 62 scoreLabel.gameObject.SetActive(false); 63 coinLabel.gameObject.SetActive(false); 64 if (Input.GetKeyDown("space")) 65 { 66 //ゲームシーンを読み込み 67 SceneManager.LoadScene("Game"); 68 } 69 if (Input.GetKeyDown("right") || Input.GetKeyDown("left")) 70 { 71 //タイトルシーンを読み込み 72 SceneManager.LoadScene("Title"); 73 } 74 } 75 break; 76 } 77 } 78 79 // Update is called once per frame 80 void Update() 81 { 82 83 } 84 85 void Ready() 86 { 87 state = State.Ready; 88 //オブジェクトを無効にする 89 EnemyGenerator.SetActive(false); 90 CoinGenerator.SetActive(false); 91 //ラベル更新 92 scoreLabel.text = "Score : " + 0; 93 StateLabel.gameObject.SetActive(true); 94 StateLabel.text = "READY"; 95 coinLabel.text = "Coin : " + 0; 96 } 97 void GameStart() 98 { 99 state = State.Play; 100 //オブジェクトを有効にする 101 EnemyGenerator.SetActive(true); 102 CoinGenerator.SetActive(true); 103 //ラベル更新 104 StateLabel.gameObject.SetActive(false); 105 StateLabel.text = ""; 106 time = time * Time.deltaTime * -1; 107 } 108 void GameOver() 109 { 110 state = State.GameOver; 111 //ラベル更新 112 StateLabel.gameObject.SetActive(true); 113 //オブジェクトを無効にする 114 EnemyGenerator.SetActive(false); 115 CoinGenerator.SetActive(false); 116 StateLabel.text = "GAMEOVER"; 117 //ハイスコアを更新 118 if (PlayerPrefs.GetInt("ScoreHighScore") < score) 119 { 120 PlayerPrefs.SetInt("ScoreHighScore", score); 121 } 122 PlayerPrefs.SetInt("Totalcoin", coin + PlayerPrefs.GetInt("Totalcoin")); 123 endscoreLabel.text = "Score : " + score; 124 endcoinLabel.text = "Coin : " + coin; 125 126 } 127 128 public void IncreaseScore() 129 { 130 if (playcon.IsDead()) return; 131 score += 50; 132 //スコア更新 133 scoreLabel.text = "Score : " + score; 134 PlayerPrefs.SetInt("NowScore", score); 135 } 136 public void IncreasePlayerScore() 137 { 138 if (playcon.IsDead()) return; 139 score += 10; 140 //スコア更新 141 scoreLabel.text = "Score : " + score; 142 PlayerPrefs.SetInt("NowScore", score); 143 } 144 public void IncreasCoin() 145 { 146 if (playcon.IsDead()) return; 147 coin++; 148 coinLabel.text = "Coin : " + coin; 149 } 150 151 public void IncreaseTime() 152 { 153 time++; 154 TimeLabel.text = "Time : " + time; 155 } 156 157 public void OnClick() 158 { 159 //タイトルシーンを読み込み 160 SceneManager.LoadScene("Title"); 161 } 162 163 public void OnRetry() 164 { 165 SceneManager.LoadScene(SceneManager.GetActiveScene().name); 166 } 167} 168

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/15 07:06
2020/01/15 07:34 編集
退会済みユーザー
2020/01/15 08:31
2020/01/15 08:37 編集
退会済みユーザー
2020/01/15 08:41
2020/01/15 09:02 編集