前提・実現したいこと
敵を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
回答2件
あなたの回答
tips
プレビュー