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

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

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

Q&A

解決済

2回答

1112閲覧

void Update内のVector3 posのデータをIEnumerator Start内で実行したいのですがどうすればいいのか分かりません。

yuusukk

総合スコア29

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

0グッド

0クリップ

投稿2019/03/01 14:23

前提・実現したいこと

void Update内のVector3 posのデータをIEnumerator Start内で実行したいのですがどうすればいいのか分かりません。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AType : MonoBehaviour 6{ 7 public GameObject bullet; 8 9 void Update() 10 { 11 Vector3 pos = this.gameObject.transform.position; 12 } 13 14 IEnumerator Start() 15 { 16 while (true) 17 { 18 Instantiate(bullet, new Vector3(pos.x, pos.y + 0.5f, pos.z), transform.rotation); 19 yield return new WaitForSeconds(0.05f); 20 } 21 } 22}

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

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

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

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

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

guest

回答2

0

ベストアンサー

posの定義位置をUpdateの外に出してやれば、UpdateStartの両方からアクセスできるかと思います。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AType : MonoBehaviour 6{ 7 public GameObject bullet; 8 9 private Vector3 pos; 10 11 void Update() 12 { 13 pos = this.gameObject.transform.position; 14 } 15 16 IEnumerator Start() 17 { 18 while (true) 19 { 20 Instantiate(bullet, new Vector3(pos.x, pos.y + 0.5f, pos.z), transform.rotation); 21 yield return new WaitForSeconds(0.05f); 22 } 23 } 24}

あるいは、ご提示のコードを拝見した限りではUpdate内で座標を取得しなくとも、Start内だけで完結しそうに思うのですがいかがでしょうか?

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AType : MonoBehaviour 6{ 7 public GameObject bullet; 8 9 /* 10 void Update() 11 { 12 pos = this.gameObject.transform.position; 13 } 14 */ 15 16 IEnumerator Start() 17 { 18 while (true) 19 { 20 Vector3 pos = transform.position; 21 Instantiate(bullet, new Vector3(pos.x, pos.y + 0.5f, pos.z), transform.rotation); 22 yield return new WaitForSeconds(0.05f); 23 } 24 } 25}

投稿2019/03/01 16:07

Bongo

総合スコア10807

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

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

yuusukk

2019/03/02 01:18

ご丁寧にありがとうございました
guest

0

こんな感じでどうですか?

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AType : MonoBehaviour 6{ 7 public GameObject bullet; 8 9 IEnumerator Start() 10 { 11 while (true) 12 { 13 Vector3 pos = this.gameObject.transform.position; 14 Instantiate(bullet, new Vector3(pos.x, pos.y + 0.5f, pos.z), transform.rotation); 15 yield return new WaitForSeconds(0.05f); 16 } 17 } 18}

あと、何度も使うスクリプトであればTransformWaitForSecondsをキャッシュ(変数に代入しておく)するとパフォーマンス上がるのでおすすめです。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AType : MonoBehaviour 6{ 7 public GameObject bullet; 8 9 Transform Transform; 10 static readonly WaitForSeconds waitTime = new WaitForSeconds(0.05f); 11 12 IEnumerator Start() 13 { 14 Transform = transform; 15 16 while (true) 17 { 18 Vector3 pos = Transform.position; 19 pos.y += 0.5f; 20 Instantiate(bullet, pos, Transform.rotation); 21 yield return waitTime; 22 } 23 } 24}

投稿2019/03/01 16:17

IShix

総合スコア1724

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

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

yuusukk

2019/03/02 01:18

ご丁寧にありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問