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

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

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

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Unity

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

Q&A

解決済

2回答

16477閲覧

【Unity2D】Visual Studioのエラー「オブジェクト参照が必要です」が解決できない

sumikko6210

総合スコア138

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Unity

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

0グッド

0クリップ

投稿2017/09/14 06:51

編集2017/09/14 06:53

###前提・実現したいこと
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

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

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

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

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

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

guest

回答2

0

static関数内でstaticでない変数にアクセスすることはできません。必ず明示的にインスタンス.変数名でアクセスする必要があります。

修正案

Group.cs内、isValidGridPos

public static bool isValidGridPos(GameObject target) { Transform tr = target.transform foreach (Transform child in tr) { Vector2 v = Grid.roundVec2(child.position); if (!Grid.insideBorder(v)) return false; if (Grid.grid[(int)v.x, (int)v.y] != null && Grid.grid[(int)v.x, (int)v.y].parent != tr) return false; } return true; }

BattleManager.cs内、Update

void Update (){ if(Group.isValidGridPos(GameObject.Find("Groupをアタッチしたオブジェクトの名前"))) { enemy.Damage(10.0f); } }

投稿2017/09/14 07:45

編集2017/09/14 07:53
frodo821

総合スコア322

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

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

0

ベストアンサー

static関数内では、transformを使うことは出来ません。
複数あった場合にどのtransformが使えばいいのか分からないためです。

すでにシーン上に存在するGroupを取得したい場合、単体ならFindObjectOfType<Group>()を、複数存在する場合はFindObjectsOfType<Group>()を使ってください。


追記:
frodo821さんが修正案を挙げておりますが、そもそもisValidGridPos()をstaticにすること自体が適切ではない気がします。
なので、私の修正案としては、以下のようになります。
Groupは複数あるつもりで書きました。
Groupが1つの場合でも動作をするとは思いますが、その場合はFindObjectOfType<Group>()を使ったほうが直感的でしょう。

Group.cs

C#

1 public bool isValidGridPos() // staticを削除 2 { 3 //(中略) 4 }

BattleManager.cs

C#

1public class BattleManager : MonoBehaviour { 2 3 //メンバ 4 public Enemy enemy; 5 private Group[] groups; // 追加 6 7 // Use this for initialization 8 void Start () { 9 groups = FindObjectsOfType<Group>(); // 追加(重い処理なので、Update()ではなく、ここで行う) 10 } 11 12 // Update is called once per frame 13 void Update (){ 14 foreach (Group group in groups) // foreachループを追加(全てのGroupに対して実行する) 15 { 16 if(group.isValidGridPos())//ここに関数を渡したい 17 { 18 enemy.Damage(10.0f); 19 } 20 } 21 } 22}

投稿2017/09/14 07:44

編集2017/09/14 08:14
fiveHundred

総合スコア9739

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問