質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2117閲覧

Unity 2Dゲームの流れてくるはずの障害物が流れてこない

0623_top

総合スコア8

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/12/17 20:11

編集2019/12/17 20:19

問題点

私は現在、Unityで悪路王という2Dゲームを制作しています。
すでに何周かしていますが、何度やっても同じ結果になってしまいます。
※バージョンは、2018.1.0です。

  • Wallオブジェクトがゲーム開始画面の時点で、1つだけ流れてしまい、クリックし、ゲームスタートしてからは一切流れてこない。
  • 上のものと同じスクリプトで動作するようになっていますが、黄色いコインも上の障害物と同じ動作をしてしまいます。

スタート前
イメージ説明
コインと障害物が流れていく…
イメージ説明
クリックし、スタートすると、黒いキャラが登場し、障害物とコインは出てこない…
イメージ説明

現状のワークスペース

イメージ説明
現状このような感じですが、どこが間違っているかがわからないため、とりあえずこの画像を貼らせていただきますが、もしどの辺が怪しい!などがありましたら、細かい画像などを追加致しますので、ご教示いただければ幸いです。

スクリプト

文字制限があるため、怪しいファイルのみ転載してます。

GameManager

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5public class GameManager : MonoBehaviour 6{ 7 //インスペクタに表示しないpublic変数 8 [System.NonSerialized] 9 public int level = 1; 10 [System.NonSerialized] 11 public int score = 0; 12 [System.NonSerialized] 13 public bool playing = false; 14 //インスペクタに表示するprivate変数 15 [SerializeField] 16 GameObject player; 17 [SerializeField] 18 int levelUpTime = 6; //レベルアップするまでの時間 19 [SerializeField] 20 int maxLevel = 10; 21 [SerializeField] 22 float addScoreInterval = 6; //スコア加算間隔 23 [SerializeField] 24 int addScoreCofficient = 1; //スコア加算係数 25 [SerializeField] 26 float playerSpawnX = -7.0f; 27 [SerializeField] 28 float playerSpawnY = 3.0f; 29 //インスペクタに表示しない変数 30 int highScore = 0; 31 string highScoreKey = "highScore"; 32 //UI取得用 33 [SerializeField] 34 Image titleLogoImg; 35 [SerializeField] 36 Text clickStartTxt; 37 [SerializeField] 38 Text gameOverTxt; 39 [SerializeField] 40 Text tryAgainTxt; 41 [SerializeField] 42 Text scoreLabelTxt; 43 [SerializeField] 44 Text scoreNumTxt; 45 [SerializeField] 46 Text highLabelTxt; 47 [SerializeField] 48 Text highNumTxt; 49 //初期化 50 void Start() 51 { 52 53 } 54 55 //毎フレームの処理 56 void Update() 57 { 58 //ゲーム中の処理 59 if (playing) 60 { 61 if (highScore < score) 62 { 63 highScore = score; 64 } 65 //スコアを表示 66 scoreNumTxt.text = score.ToString("d6"); 67 highNumTxt.text = highScore.ToString("d6"); 68 } 69 //タイトルまたはゲームオーバー 70 else 71 { 72 //クリックされたらゲーム開始 73 if (Input.GetMouseButtonDown(0)) 74 { 75 StartGame(); 76 } 77 } 78 } 79 //ゲーム開始処理 80 void StartGame() 81 { 82 //ガベージコレクション実行 83 System.GC.Collect(); 84 //初期化 85 score = 0; 86 level = 1; 87 //ハイスコア読み込み 88 highScore = PlayerPrefs.GetInt(highScoreKey, 0); 89 //タイトルまたはゲームオーバー画面非表示 90 titleLogoImg.enabled = false; 91 clickStartTxt.enabled = false; 92 gameOverTxt.enabled = false; 93 tryAgainTxt.enabled = false; 94 //スコアを表示 95 scoreLabelTxt.enabled = true; 96 scoreNumTxt.enabled = true; 97 highLabelTxt.enabled = true; 98 highNumTxt.enabled = true; 99 playing = true; 100 //もし地形やコインが残ってたら消去 101 DeleteObjects("Wall"); 102 DeleteObjects("Item"); 103 //プレイヤー生成 104 Instantiate(player, new Vector3(playerSpawnX, playerSpawnY, 0.0f), Quaternion.identity); 105 //ゲーム開始後、一定時間ごとの処理 106 InvokeRepeating("LevelUp", levelUpTime, levelUpTime); 107 InvokeRepeating("AddScoreByTime", addScoreInterval, addScoreInterval); 108 } 109 //ゲームオーバー処理 110 public void GameOver() 111 { 112 CancelInvoke(); 113 playing = false; 114 // ハイスコアを保存 115 PlayerPrefs.SetInt(highScoreKey, highScore); 116 PlayerPrefs.Save(); 117 //ゲームオーバー画面表示 118 gameOverTxt.enabled = true; 119 tryAgainTxt.enabled = true; 120 } 121 //スコア加算 122 void AddScoreByTime() 123 { 124 score += addScoreCofficient * (10 + level - 1); 125 Debug.Log(score); 126 } 127 //レベルアップ 128 void LevelUp() 129 { 130 if (level < maxLevel) 131 { 132 level++; 133 Debug.Log(level); 134 } 135 } 136 //ゲームオブジェクトの一括削除 137 void DeleteObjects(string tag) 138 { 139 GameObject[] deleteObjects = GameObject.FindGameObjectsWithTag(tag); 140 if (deleteObjects.Length > 0) 141 { 142 foreach (GameObject obj in deleteObjects) 143 { 144 Destroy(obj); 145 } 146 } 147 } 148}

Emitter

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class Emitter : MonoBehaviour 5{ 6 [SerializeField] 7 float emitWaitTimeMin = 0.6f; 8 [SerializeField] 9 float emitWaitTimeMax = 1.0f; 10 [SerializeField] 11 float intervalCofficient = 0.98f; //レベルアップしたとき、生成間隔を何倍するかの係数 12 [SerializeField] 13 float wallSpawnX = 13.0f; 14 [SerializeField] 15 float wallSpawnY = -2.5f; 16 [SerializeField] 17 float coinSpawnX = 11.0f; 18 [SerializeField] 19 float coinSpawnY = 5.0f; 20 [SerializeField] 21 GameObject wall; 22 [SerializeField] 23 GameObject coin; 24 [SerializeField] 25 GameManager gameManager; 26 bool emitterEnabled = true; 27 int cullentLevel; 28 float emitWaitTimeMinNow; 29 float emitWaitTimeMaxNow; 30 //毎フレームの処理 31 void Update() 32 { 33 //タイトルまたはゲームオーバー画面 34 if (!gameManager.playing) 35 { 36 //初期化 37 cullentLevel = gameManager.level; 38 emitWaitTimeMinNow = emitWaitTimeMin; 39 emitWaitTimeMaxNow = emitWaitTimeMax; 40 } 41 //プレイ中の処理 42 if (emitterEnabled && gameManager.playing) 43 { 44 //生成処理 45 Instantiate(wall, new Vector3(wallSpawnX, wallSpawnY, transform.position.z), Quaternion.identity); 46 Instantiate(coin, new Vector3(coinSpawnX, coinSpawnY, transform.position.z), Quaternion.identity); 47 StartCoroutine(EmitWait()); 48 //レベルアップ時に生成速度Up 49 if (gameManager.level != cullentLevel) 50 { 51 emitWaitTimeMinNow *= intervalCofficient; 52 emitWaitTimeMaxNow *= intervalCofficient; 53 cullentLevel = gameManager.level; 54 } 55 } 56 } 57 //生成を待つ処理 58 IEnumerator EmitWait() 59 { 60 emitterEnabled = false; 61 float emitWaitTime = Random.Range(emitWaitTimeMinNow, emitWaitTimeMaxNow); 62 yield return new WaitForSeconds(emitWaitTime); 63 emitterEnabled = true; 64 } 65}

Wall

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class Wall : MonoBehaviour 5{ 6 [SerializeField] 7 float speed = 10.0f; 8 [SerializeField] 9 float minScaleX = 4.0f; 10 [SerializeField] 11 float minScaleY = 3.0f; 12 [SerializeField] 13 float maxScaleX = 6.0f; 14 [SerializeField] 15 float maxScaleY = 7.0f; 16 bool inCamera = false; 17 Rigidbody2D rigidBody2D; 18 //初期化 19 void Start() 20 { 21 //コンポーネントを取得 22 rigidBody2D = GetComponent<Rigidbody2D>(); 23 //壁の長さをランダムに変更 24 float scaleX = Random.Range(minScaleX, maxScaleX); 25 float scaleY = Random.Range(minScaleY, maxScaleY); 26 transform.localScale = new Vector3(scaleX, scaleY, transform.localScale.z); 27 } 28 //物理演算 29 void FixedUpdate() 30 { 31 rigidBody2D.velocity = Vector2.left * speed; 32 } 33 //消去処理 34 public void DestroyBlock() 35 { 36 Destroy(gameObject); 37 } 38 //カメラ内外の判定処理 39 void OnBecameVisible() 40 { 41 inCamera = true; 42 } 43 void OnBecameInvisible() 44 { 45 //一度画面内に入って、また出たときに消去する 46 if (inCamera) 47 { 48 DestroyBlock(); 49 } 50 } 51}

Coin

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4public class Coin : MonoBehaviour 5{ 6 [SerializeField] 7 float speed = -10.0f; 8 Rigidbody2D rigidBody2D; 9 bool inCamera = false; 10 //初期化 11 void Start() 12 { 13 rigidBody2D = GetComponent<Rigidbody2D>(); 14 } 15 //物理演算 16 void FixedUpdate() 17 { 18 rigidBody2D.velocity = new Vector2(speed, rigidBody2D.velocity.y); ; 19 } 20 //カメラ内外の判定処理 21 void OnBecameVisible() 22 { 23 inCamera = true; 24 } 25 void OnBecameInvisible() 26 { 27 if (inCamera) 28 { 29 Destroy(gameObject, 1.0f); 30 } 31 } 32}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Y0241-N

2019/12/18 00:30

コンソールにエラー文が表示されていますが、他にもエラーが出ていませんか?
sakura_hana

2019/12/18 02:04

「MissingReferenceException」は削除したオブジェクトにアクセスし続けているというエラーです。 teratailでも同じ質問が出ているのでこのワードで調べてください。 (多分WallとCoinがシーン上に存在しているのが問題かなと思います)
0623_top

2019/12/18 04:46

Y0241-N様 ご指摘ありがとうございます。 他のエラーについてですが、 NullReferenceException: Object reference not set to an instance of an object Emitter.Update () (at Assets/Scripts/Emitter.cs:34) というエラーが何度も出ていました。
0623_top

2019/12/18 04:48

sakura_hana様 ご指摘ありがとうございます。 調べて試してみます。 ありがとうございます。
guest

回答1

0

自己解決

Emitterのスクリプトに、GameManagerスクリプトをアタッチしていないことが原因でうまく動作していませんでした。
ご回答いただいたみなさま、ありがとうございました。

投稿2019/12/25 01:01

0623_top

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問