質問するログイン新規登録

質問編集履歴

1

prefab用のスクリプトを追記いたしました。

2021/05/17 06:38

投稿

nori0519
nori0519

スコア16

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  prefabで生成したgameobjectをスクリプトで呼び出すことはできるのでしょうか。
2
2
  以下のような呼び出し方で試してみたのですが、うまくいきませんでした。
3
- ```C#
3
+ ```BulletOutput
4
4
  GameObject bulletPrefab;
5
5
  void Start()
6
6
  {
@@ -15,4 +15,31 @@
15
15
       Debug.Log("x:"+pos.x+"y:"+pos.y);
16
16
  }
17
17
  ```
18
+ ```BulletGenerator
19
+ public class BulletGenerator2 : MonoBehaviour
20
+ {
21
+ //弾オブジェクト
22
+ public GameObject bulletPrefab;
23
+ //プレイヤオブジェクト
24
+ GameObject player;
25
+ // Start is called before the first frame update
26
+ void Start()
27
+ {
28
+ //ヒエラルキーからプレイヤー2オブジェクト検索
29
+ this.player = GameObject.Find("player2");
30
+ }
31
+
32
+ // Update is called once per frame
33
+ void Update()
34
+ {
35
+ if (Input.GetKey(KeyCode.Space))
36
+ {
37
+ //弾を定義
38
+ GameObject go = Instantiate(bulletPrefab) as GameObject;
39
+ // 弾をプレイヤーの中心座標から生成
40
+ go.transform.position = new Vector3(this.player.transform.position.x, this.player.transform.position.y - 1.0f, 0);
41
+ }
42
+ }
43
+ }
44
+ ```
18
45
  ご教授いただければ幸いです。よろしくお願いいたします。