unity c#初心者です。
他のスクリプトで作った変数を参照したいのですが、上手くいかず困っています。
今、ボールに下記「Ball_move.cs」スクリプトをつけてます。
public
1{ 2 public string myName; 3 public float distancePerFrame; 4 public float paddlePostion; 5 public int x_dir = 1; 6 public int y_dir = 1; 7 8‥中略‥ 9 10 void Update() 11 { 12 transform.Translate(x_dir * distancePerFrame, y_dir * distancePerFrame, 0); 13 } 14 15 16 void OnTriggerEnter(Collider c) 17 { 18 if (c.gameObject.tag == "yoko_filed") 19 { 20 x_dir = x_dir * -1; 21 } 22 if (c.gameObject.tag == "filed") 23 { 24 y_dir = y_dir * -1; 25 } 26 return; 27 } 28コード
このコードの
public int x_dir = 1;
public int y_dir = 1;
を、ブロックにつけた「Triger.cs」から「Ball_move.cs」スクリプトの変数を参照してBall_move.csのx_dir、y_dirの値を変えたいのですが上手くいかず困っています。
下記URLを参照して作ったのですが、上手くいかず困ってます。
デバックで、OnTriggerEnterまで、飛ぶところは、上手くいきました。
public
1 public Ball_move ball_move_script; 2 public GameObject B_obj; 3 // Use this for initialization 4 void Start () { 5 6 } 7 8 // Update is called once per frame 9 void Update () { 10 11 } 12 13 void OnTriggerEnter(Collider c) 14 { 15 if (c.gameObject.tag == "ball") 16 { 17 int b_x_dir; 18 int b_y_dir; 19 b_x_dir = ball_move_script.x_dir; 20 b_y_dir = ball_move_script.y_dir; 21 22 b_x_dir = b_x_dir * -1; 23 b_y_dir = b_y_dir * -1; 24// Destroy(B_obj); 25 } 26 27 28 return; 29 } 30} 31 32コード
あと、下記エラーが出ます。
NullReferenceException: Object reference not set to an instance of an object
Triger.OnTriggerEnter (UnityEngine.Collider c) (at Assets/Triger.cs:23)
何卒、ご教授のほどよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/10 05:46
2018/05/10 06:29
2018/05/10 08:45