###前提・実現したいこと
Unity 2Dでゲームを作成しており、bool関数をpublicにして別のファイルに関数を渡したいのですが
「静的でないフィールド、メソッド、またはプロパティ"Component.transform"でオブジェクト参照が必要です」と警告が出て、解決できず困っています
###発生している問題・エラーメッセージ
Group.csの15,23行目のtransformがエラーになり
静的でないフィールド、メソッド、またはプロパティ"Component.transform"でオブジェクト参照が必要です
と警告が出ます
###該当のソースコード
Group.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Group : MonoBehaviour 6{ 7 private Vector3 screenPoint; 8 private Vector3 offset; 9 private Count count; 10 11 12 //座標が範囲内であるか,Gridに値があるかどうかを判定 13 //この関数をpublicにしたい 14 public static bool isValidGridPos() 15 { 16 foreach (Transform child in transform)//エラー 17 { 18 Vector2 v = Grid.roundVec2(child.position); 19 20 if (!Grid.insideBorder(v)) 21 return false; 22 23 if (Grid.grid[(int)v.x, (int)v.y] != null && 24 Grid.grid[(int)v.x, (int)v.y].parent != transform)//エラー 25 return false; 26 } 27 return true; 28 } 29 30 void updateGrid() 31 { 32 for (int y = 0; y < Grid.h; ++y) 33 for (int x = 0; x < Grid.w; ++x) 34 if (Grid.grid[x, y] != null) 35 if (Grid.grid[x, y].parent == transform) 36 Grid.grid[x, y] = null; 37 38 foreach (Transform child in transform) 39 { 40 Vector2 v = Grid.roundVec2(child.position); 41 Grid.grid[(int)v.x, (int)v.y] = child; 42 } 43 } 44 45 //オブジェクトをクリックする 46 void OnMouseDown() 47 { 48 // マウスカーソルは、スクリーン座標なので、 49 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 50 51 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 52 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 53 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 54 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 55 56 57 } 58 59 //オブジェクトをドラッグする 60 void OnMouseDrag() 61 { 62 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 63 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 64 transform.position = currentPosition; 65 } 66 67 //オブジェクトをドロップする 68 void OnMouseUp() 69 { 70 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 71 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 72 73 //currentPositionが範囲内であるか,Gridに値があるかどうかを判定 74 if (isValidGridPos()) 75 { 76 transform.position = Grid.roundVec2(currentPosition); 77 count.countUp(); 78 Grid.deleteFullRows(); 79 Grid.deleteFullLines(); 80 81 //指定範囲内ではブロックの当たり判定を無くす 82 BoxCollider2D collider = GetComponent<BoxCollider2D>(); 83 collider.enabled = false; 84 85 //次のブロックを呼び出す 86 FindObjectOfType<Spawner>().spawnNext(); 87 88 89 } 90 else 91 { 92 //Spawnerオブジェクトの座標を取る 93 transform.position = GameObject.Find("Spawner").transform.position; 94 95 } 96 97 } 98 99 100 // Use this for initialization 101 void Start() 102 { 103 count = FindObjectOfType<Count>(); 104 } 105 106 // Update is called once per frame 107 void Update() 108 { 109 if (isValidGridPos()) 110 { 111 // It's valid. Update grid. 112 updateGrid(); 113 } 114 } 115} 116
BattleManager.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BattleManager : MonoBehaviour { 6 7 //メンバ 8 public Enemy enemy; 9 10 // Use this for initialization 11 void Start () { 12 13 } 14 15 // Update is called once per frame 16 void Update (){ 17 if(Group.isValidGridPos())//ここに関数を渡したい 18 { 19 enemy.Damage(10.0f); 20 } 21 } 22} 23
回答よろしくお願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
unity 5.6.2f1

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。