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

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

新規登録して質問してみよう
ただいま回答率
85.51%
Unity

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

Q&A

解決済

3回答

5214閲覧

Unityでほかのスクリプト参照

Aya_program

総合スコア30

Unity

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

0グッド

2クリップ

投稿2016/07/11 21:23

編集2016/07/12 07:46

Plefab PA と PB が存在し
Script SA と SB があります

PAにSAとPBをアタッチします
PBにSBをアタッチします
SAのスクリプトよりPAの子オブジェクトにPBが生成されます

このPBからSAのスクリプトを読み込み 変数 を参照したいのですがうまくいかないです

SA のスクリプト

C#

1using UnityEngine; 2using System.Collections; 3 4public class AngelScript : MonoBehaviour { 5 6 public bool P1db_attckon = false; 7 public bool P2db_attckon = false; 8 9 private Transform player1; 10 private Transform player2; 11 public GameObject AngelSword; 12 13 public bool toplayer; 14 15 // Use this for initialization 16 void Start () { 17 18 // 始めにプレイヤーの位置を取得できるようにする 19 player1 = GameObject.FindWithTag("Player1").transform; 20 player2 = GameObject.FindWithTag("Player2").transform; 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 if (P1db_attckon) 27 { 28 toplayer = true; 29 P1_AttackAngel(); 30 P1db_attckon = false; 31 } 32 33 if (P2db_attckon) 34 { 35 toplayer = false; 36 P2_AttackAngel(); 37 P2db_attckon = false; 38 } 39 } 40 41 private void P1_AttackAngel() 42 { 43 44 GameObject obj = Instantiate(AngelSword, player1.transform.position + new Vector3(0,8f,3f), player1.transform.rotation) as GameObject; 45 obj.transform.parent = this.transform; 46 47 } 48 49 private void P2_AttackAngel() 50 { 51 52 GameObject obj = Instantiate(AngelSword, player2.transform.position + new Vector3(0, 8f, 3f), player2.transform.rotation) as GameObject; 53 obj.transform.parent = this.transform; 54 55 } 56 57 58 59}

SBのスクリプト

C#

1using UnityEngine; 2using System.Collections; 3 4public class AngelSword : MonoBehaviour { 5 6 //使いたいメソッドや変数のあるスクリプト名 7 AngelScript angelscript; 8 //そのスクリプトが使われているGameObjを入れるための物 9 public GameObject angel; 10 11 private Transform player; 12 private Transform player1; 13 private Transform player2; 14 15 private const float waitTime1 = 3; 16 private const float waitTime2 = 5; 17 private const float waitTime3 = 8; 18 private const float waitTime4 = 8.15f; 19 20 private float rotationSmooth = 10.0f; // 回転速度 21 private float countTime = 0; 22 23 bool flag = true; 24 public bool debug = false; 25 26 // Use this for initialization 27 void Start () { 28 29 angelscript = angel.GetComponent<AngelScript>(); 30 31 // 始めにプレイヤーの位置を取得できるようにする 32 //player = GameObject.FindWithTag("Player").transform; 33 player1 = GameObject.FindWithTag("Player1").transform; 34 player2 = GameObject.FindWithTag("Player2").transform; 35 } 36 37 // Update is called once per frame 38 void Update () { 39 40 if (angelscript.toplayer) debug = true; 41 else debug = false; 42 43 44 45 //毎フレームカウント 46 countTime += Time.deltaTime; 47 48 if (countTime < waitTime1) 49 { 50 51 } 52 else if((countTime >= waitTime1) && (countTime < waitTime2)) 53 { 54 //angle変数(x,y,z)の数値にTime.deltaTimeを掛けて毎フレームごとに移動するようにする 55 Vector3 angle = new Vector3(0, 50f, 0); 56 transform.Rotate(angle); 57 58 59 } 60 else if((countTime >= waitTime2) && (countTime < waitTime3)) 61 { 62 if (angelscript.toplayer) 63 { 64 // プレイヤーの方向を向く 65 Quaternion targetRotation = Quaternion.LookRotation(player1.position - transform.position); 66 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth); 67 } 68 else 69 { 70 // プレイヤーの方向を向く 71 Quaternion targetRotation = Quaternion.LookRotation(player2.position - transform.position); 72 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth); 73 } 74 // プレイヤーの方向を向く 75 //Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position); 76 //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth); 77 78 79 } 80 else if((countTime >= waitTime3) && (countTime < waitTime4)) 81 { 82 if (flag) 83 { 84 flag = false; 85 if (angelscript.toplayer) 86 { 87 // プレイヤーの方向を向く 88 Quaternion targetRotation = Quaternion.LookRotation(player1.position - transform.position); 89 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth); 90 transform.Rotate(new Vector3(1, 0, 0), 90); 91 } 92 else{ 93 // プレイヤーの方向を向く 94 Quaternion targetRotation = Quaternion.LookRotation(player2.position - transform.position); 95 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth); 96 transform.Rotate(new Vector3(1, 0, 0), 90); 97 } 98 // プレイヤーの方向を向く 99 // Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position); 100 // transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth); 101 //transform.Rotate(new Vector3(1, 0, 0), 90); 102 } 103 } 104 else 105 { 106 if (angelscript.toplayer) 107 { 108 Vector3 direction = (player1.transform.position - transform.position).normalized; 109 this.gameObject.GetComponent<Rigidbody>().AddForce(transform.up * 20, ForceMode.Impulse); 110 Destroy(this.gameObject.transform.root.gameObject, 1f); 111 } 112 else 113 { 114 Vector3 direction = (player2.transform.position - transform.position).normalized; 115 this.gameObject.GetComponent<Rigidbody>().AddForce(transform.up * 20, ForceMode.Impulse); 116 Destroy(this.gameObject.transform.parent.gameObject, 1f); 117 } 118 //Vector3 direction = (player.transform.position - transform.position).normalized; 119 //this.gameObject.GetComponent<Rigidbody>().AddForce(transform.up * 20, ForceMode.Impulse); 120 //Destroy(this.gameObject.transform.root.gameObject,1f); 121 } 122 123 124 125 } 126 127} 128

読み込みたい変数はangelscript.toplayerです
デバックで確認したところ
SAの P1db_attckon を TRUE に変更しても
SBの debug は FALSE のままでした
何か根本的なことで間違っているのでしょうか?


追記

プロジェクトビューには Prefab PA PB SA SB
ヒエラルキービューには PA
PA にスクリプトSAをアタッチ
プロジェクトビューのPBのAngelには プロジェクトビューの PA を入れています

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

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

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

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

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

urahimono

2016/07/11 22:18

いくつか質問させてください。 1.初期状態として、PAとPBはProjectビュー上にPrefabとして存在して、Hierarchyビュー上にはPAだけが存在する状況で間違いないですか。 2.PBのSBの変数である`angel`には何のオブジェクトを指定していますか。Projectビュー上のPAを指定しているのでしょうか。
guest

回答3

0

ベストアンサー

Projectビュー上にあるPAをPA、PBをPB、Hierarchyビュー上にあるPAをPA'、PBをPB'の名前で表現するとして、
Hierarchyビュー上で
PA'AngelScriptが生成したPB'angel変数が指しているGameObjectはProjectビューにあるPA
になります。
そのため**PA'**の変数の状態を正しく取得できない状況にあります。
イメージ説明

改善案と致しましては、AngelScriptが**PB'**を生成した際に、**PB'AngelSwordangel変数の値にPA'**のGameObjectを指定する方法が考えられます。

csharp

1private void P1_AttackAngel() 2{ 3 4 GameObject obj = Instantiate( AngelSword, player1.transform.position + new Vector3( 0, 8f, 3f ), player1.transform.rotation ) as GameObject; 5 obj.transform.parent = this.transform; 6 7 var sword = obj.GetComponent<AngelSword>(); 8 if( sword != null ) 9 { 10 sword.angel = gameObject; 11 } 12} 13 14private void P2_AttackAngel() 15{ 16 17 GameObject obj = Instantiate( AngelSword, player2.transform.position + new Vector3( 0, 8f, 3f ), player2.transform.rotation ) as GameObject; 18 obj.transform.parent = this.transform; 19 20 var sword = obj.GetComponent<AngelSword>(); 21 if( sword != null ) 22 { 23 sword.angel = gameObject; 24 } 25}

ご参考になればと思います。

投稿2016/07/12 21:56

urahimono

総合スコア714

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

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

Aya_program

2016/07/12 23:59

詳しくありがとうございます! 原因がわかってすっきりしました!
guest

0

変数の宣言を
public static 〇〇
で宣言すると
SBのスクリプトでSA.〇〇というように使えるようになります。

投稿2016/07/12 08:58

ysksar20150924

総合スコア19

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

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

Aya_program

2016/07/12 21:21

staticで行うことにしました ありがとうございます
guest

0

現在 AngelSword の Start には angel.GetComponent<AngelScript>() とありますが
この angelは「どこかで設定済み」でしょうか?
ここに提示された範囲内では未設定のように見えますが…そこは大丈夫でしょうか?
angelが未設定ならエラーがでると思うのでそこは問題ないですかね…
angelがnullならangelscriptも結果的にnullになると思うのでangelscript.toplayerの参照でも
エラーになるはずで…でもエラーについては何も触れられていませんので、という事はこれは
部分的に抽出した一部って解釈をすればいいでしょうか…?
ひとまずangelが正しく設定されているかを確認してみてはどうでしょうか?

投稿2016/07/12 01:20

HiroshiWatanabe

総合スコア2160

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問