前提・実現したいこと
敵をPrefabにて生成して、その敵(Enemy)から銃弾(EnemyBullet)が発生しない。
発生している問題・エラーメッセージ
敵はPrefabにて生成できるが、その生成した敵から銃弾が発生しない。
追加問題
1つ目の画像が同時に生成されたEnemy(Prefab)の個数であり、2つ目の画像はそのうちの1つのPrefabの持つスクリプトであり、3つ目はそのうちの2つのPrefabが持つスクリプトである。何故同時に3つもPrefabが生成されるのか、1つ目のPrefabのスクリプトはなぜ効果を失ってしまったのかも不明である。これらの原因も知っている方がいましたら、教えていただけると幸いです。



C#
ソースコード
敵の生成コード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyGenerator : MonoBehaviour { public GameObject EnemyPrefab; float span = 25.0f; float delta = 0; private const float V = 150f; private const float W = 50.0f; public GameObject Player; private Transform Playertrans; // Start is called before the first frame update void Start() { Playertrans = Player.GetComponent<Transform>(); } // Update is called once per frame void Update() { float Playerlong = Playertrans.position.x + V; float Playerheight = Playertrans.position.y + W; this.delta += Time.deltaTime; if (this.delta > this.span) { this.delta = 0; GameObject go = Instantiate(EnemyPrefab, new Vector3(Playerlong, Playerheight, 0.0f), Quaternion.identity); } } }
銃弾の生成コード(敵(Enemy)の目の前に生成されるように設定)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyBulletGenerator: MonoBehaviour { public GameObject EnemyBulletPrefab; float span = 1.0f; float delta = 0; private const float V = -0.5f; public GameObject Enemy; private Transform Enemytrans; // Start is called before the first frame update void Start() { Enemytrans = Enemy.GetComponent<Transform>(); } // Update is called once per frame void Update() { float Enemylong = Enemytrans.position.x + V; float Enemyheight = Enemytrans.position.y; this.delta += Time.deltaTime; if(this.delta > this.span) { this.delta = 0; GameObject go = Instantiate(EnemyBulletPrefab, new Vector3(Enemylong, Enemyheight, 0.0f), Quaternion.identity); } } }
試したこと
特に特筆すべきことはできていません。考えましたがどうすればいいかわかりませんでした。Enemyを複製して設置しても変わらず生成されませんでした。
補足情報(FW/ツールのバージョンなど)
Unity 2020.2.14f1 Personal
ソースコードは以下のように「```」で囲んでください。
```C#
public class Foo : MonoBehaviour
{
}
```
EnemyBulletGenerator.Update()が呼び出されていることは確認済みですか?
今後から囲みます。失礼しました。
Prefabではない、最初に設置した敵(Enemy)からはEnemyBulletGeneratorが呼び出され、銃弾が生成されるのですが、それ以降の敵(EnemyPrefab)からは何故かEnemyBulletGeneratorが呼び出されず、銃弾が生成されないという状態です。
質問欄は「編集」ボタンから修正できますので、「```」で囲んでください。
コードが見づらいと回答が付きづらくなりますので。
補足ですが、「"」ではなく「```」です。
Markdownの<code>タグを使ってくださいということです。
https://teratail.com/help/question-tips#questionTips3-7
丁寧に本当にありがとうございます!
teratail初心者なもので………
EnemyBulletGeneratorはどのオブジェクトにアタッチしているでしょうか?
EnemyGeneratorの方でしたらシーンのヒエラルキー上に置いた空のオブジェクトにアタッチする...といった手があるでしょうが、もしEnemyBulletGeneratorでも同じようにしているとすると現状のスクリプトでは複数の敵オブジェクトを扱うことができないのではないかと思うのですが...
EnemyBulletGeneratorは空のゲームオブジェクトにアタッチしています。
やはり無理なのでしょうか……
やり方を変えるしかないのでしょうか
回答2件
あなたの回答
tips
プレビュー
