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 を入れています
いくつか質問させてください。
1.初期状態として、PAとPBはProjectビュー上にPrefabとして存在して、Hierarchyビュー上にはPAだけが存在する状況で間違いないですか。
2.PBのSBの変数である`angel`には何のオブジェクトを指定していますか。Projectビュー上のPAを指定しているのでしょうか。
回答3件
あなたの回答
tips
プレビュー