とあるUnityの参考書を進めています。その中でどうしても解決できない問題があり、知恵を貸していただきたいです。今回の質問にはあまり関係ないと思いますが、3Dで制作している前提です。
まず、オブジェクトが二つあります。
text
1BallPrefab 2 .ボールのPrefab 3 .BallContoroller.csがアタッチされている。 4 5 6BallGenerator 7 ・空のオブジェクト 8 ・BallGenarator.csがアタッチされている。 9 ・Prefabもアタッチ済み。
次に参考書に書いてあるスクリプトです。(変数名などは変えてます)
BallController
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class BallController : MonoBehaviour 7{ 8 public void Shoot(Vector3 vector3) 9 { 10 GetComponent<Rigidbody>.AddForce(vector3); 11 } 12 13 private void OnCollisionEnter(Collision other) 14 { 15 GetComponent<Rigidbody>().isKinematic = true; 16 GetComponent<ParticleSystem>().Play(); 17 } 18}
BallGenerator
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BallGenerator : MonoBehaviour 6{ 7 public GameObject igaguriPrefab; 8 9 // Update is called once per frame 10 void Update() 11 { 12 if (Input.GetMouseButton(0)) { 13 GameObject Ball = Instantiate(BallPrefab) as GameObject; 14 Ball.GetComponent<BallController>().Shoot(new Vector3(0, 200, 2000)); 15 } 16 } 17}
やってることとしては、マウスをクリックしたらボールのRigidbody
に力を加えて奥に飛ばしているという感じです。
ただ、このスクリプトには問題があります。それはもちろんUpdate
の中でGetComponent
を使っているところです。上記のコードでは、
- 生成したオブジェクトの関数を呼び出す時
- 呼び出した関数内(
Rigidbody
コンポーネントを呼び出す時)
この2点で GetComponent
を使っています。
こちらの記事でUpdate
内で使う場合はキャッシュ化した方が良いとあったので、自分なりにキャッシュ化してみました。それが下記のコードです。
BallController
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class BallController : MonoBehaviour 7{ 8 private Rigidbody _rigidBody; 9 10 public void Shoot(Vector3 vector3) 11 { 12 _rigidBody.AddForce(vector3); 13 } 14 15 private void OnCollisionEnter(Collision other) 16 { 17 GetComponent<Rigidbody>().isKinematic = true; 18 GetComponent<ParticleSystem>().Play(); 19 } 20 21 private void Awake() 22 { 23 _rigidBody = GetComponent<Rigidbody>(); 24 }
BallGenerator
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BallGenerator : MonoBehaviour 6{ 7 public GameObject ballPrefab; 8 private GameObject _ball; 9 private BallController _ballController; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 // ここでInstantiateを使うと、クリック前にオブジェクトが生成されてしまう。 15 // _ball = Instantiate(ballPrefab) as GameObject; 16 // _ballController = _ball.GetComponent<BallController>(); 17 } 18 19 20 // Update is called once per frame 21 void Update() 22 { 23 if (Input.GetMouseButton(0)) { 24 _ball = Instantiate(ballPrefab) as GameObject; 25 _ball.GetComponent<BallController>().Shoot(new Vector3(0, 200, 2000)); 26 } 27 } 28}
呼び出した関数内(Rigidbody
コンポーネントを呼び出す時)のキャッシュ化はできました。しかし色々考えてはみたのですが、生成したオブジェクトの関数を呼び出す時のキャッシュ化ができませんでした。Start
に書いてしまうと、マウスをクリックする前にオブジェクトが生成されてしまい、期待する動きではなくなってしまいます。
割と特殊?な状況だと思うのですが、こういった場合はどのようにキャッシュ化すれば良いでしょうか?それともこのまま
妥協してUpdate
内にGetComponent
を書いてしまった方が良いのでしょうか?
※情報が足りない場合は、追加いたしますので指摘お願いします。

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/12 05:31
2019/08/12 05:41
2019/08/12 05:46
2019/08/12 06:12