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

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

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

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

Unity

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

Q&A

解決済

1回答

1150閲覧

メソッド内でのbool値の変更について

Nanmotsu

総合スコア22

C#

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

Unity

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

0グッド

0クリップ

投稿2020/06/04 00:21

unityにて図形をはめるパズルゲームを作成しています。
図形が型にはまれば、動かすことができないようにbool値を用いてスクリプトをかきましたが、条件を満たしても変わりません。

以下スクリプト

メソッドが書かれたスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GameMethodScript : MonoBehaviour 6{ 7 public int goalcount ; 8 9 10 11 12 13 // Start is called before the first frame update 14 void Start() 15 { 16 17 18 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 25 Debug.Log(goalcount); 26 } 27 28 public void grphicmethod(string name1, string name2, float buzzle, Vector3 point, bool fit) 29 { 30 if (Input.GetMouseButton(0)) 31 { 32 33 point = Camera.main.WorldToScreenPoint(transform.position); 34 Ray ray = new Ray(); 35 ray = Camera.main.ScreenPointToRay(Input.mousePosition); 36 37 RaycastHit2D hit = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction, Mathf.Infinity); 38 39 40 41 if (hit.collider.gameObject.CompareTag(name1)) 42 { 43 44 45 point = Camera.main.WorldToScreenPoint(transform.position); 46 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, point.z); 47 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint); 48 hit.collider.gameObject.transform.position = currentPosition; 49 } 50 51 } 52 53 54 55 if (GameObject.FindWithTag(name1).transform.position.x > GameObject.FindWithTag(name2).transform.position.x - buzzle 56 && GameObject.FindWithTag(name1).transform.position.x < GameObject.FindWithTag(name2).transform.position.x + buzzle 57 && GameObject.FindWithTag(name1).transform.position.y > GameObject.FindWithTag(name2).transform.position.y - buzzle 58 && GameObject.FindWithTag(name1).transform.position.y < GameObject.FindWithTag(name2).transform.position.y + buzzle) 59 60 { 61 fit = false; 62 Debug.Log("OK"); 63 GameObject.FindWithTag(name1).transform.position = GameObject.FindWithTag(name2).transform.position; 64 65 } 66 67 68 69 70 } 71 72}

メソッドを動かすスクリプト

C#

1using System.Collections; 2using System.Linq; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class GameControllerScript : MonoBehaviour 7{ 8 public Vector3 screenPoint; 9 10 public GameObject tr, cir, squ, gen, st, trgoal, cirgoal, squgoal, gengoal, stgoal; 11 12 private int a, b, c; 13 14 public bool trfit, genfit, cirfit, squfit, stfit; 15 16 public GameObject[] grphics; 17 public GameObject[] goal; 18 19 private GameMethodScript gms; 20 21 // Start is called before the first frame update 22 void Start() 23 { 24 25 grphics = new GameObject[]{ tr, cir, squ, gen, st }; 26 goal = new GameObject[] { trgoal, cirgoal, squgoal, gengoal, stgoal }; 27 28 gamestart(); 29 30 gms = GameObject 31 .FindWithTag("GameMethod") 32 .GetComponent<GameMethodScript>(); 33 34 35 36 } 37 38 39 void Update() 40 { 41 42 43 Debug.Log(trfit); 44 45 if (trfit) 46 { 47 gms.grphicmethod("tr", "trgoal", 0.2f, screenPoint, trfit); 48 } 49 gms.grphicmethod("gen", "gengoal", 0.2f, screenPoint,genfit); 50 gms.grphicmethod("cir", "cirgoal", 0.2f, screenPoint,cirfit); 51 gms.grphicmethod("squ", "squgoal", 0.2f, screenPoint,squfit); 52 gms.grphicmethod("st", "stgoal", 0.2f, screenPoint,stfit); 53 54 55 56 } 57 58 public void gamestart() 59 { 60 trfit = true; 61 genfit = true; 62 cirfit = true; 63 squfit = true; 64 stfit = true; 65 66 a = Random.Range(0, 5); 67 68 do 69 { 70 b = Random.Range(0, 5); 71 } 72 while (a == b); 73 74 do 75 { 76 c = Random.Range(0, 5); 77 78 } 79 while (a == c || b == c); 80 81 GameObject[] goal2 = { goal[a], goal[b], goal[c] }; 82 83 goal2 = goal2.OrderBy(i => System.Guid.NewGuid()).ToArray(); 84 85 86 Instantiate(grphics[a], new Vector3(-5f, -2.5f, 0f), transform.rotation); 87 Instantiate(grphics[b], new Vector3(0f, -2.5f, 0f), transform.rotation); 88 Instantiate(grphics[c], new Vector3(5f, -2.5f, 0f), transform.rotation); 89 90 Instantiate(goal2[0], new Vector3(-5f, 2f, 0f), transform.rotation); 91 Instantiate(goal2[1], new Vector3(0f, 2f, 0f), transform.rotation); 92 Instantiate(goal2[2], new Vector3(5f, 2f, 0f), transform.rotation); 93 94 } 95 96 97} 98

メソッド内の
DebugDebug.Log("OK");
は動いています。

ご教示お願い致します。

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

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

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

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

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

YAmaGNZ

2020/06/04 00:31

メソッドをvoidではなくbool値を返すようにすればいいのではないですか?
y_waiwai

2020/06/04 00:36

あなたのいうbool値というのはどれのことを言ってるんでしょうか
gentaro

2020/06/04 02:00

エスパーするとgrphicmethodとやらの第五引数あたりの話のような気がするけど、outやrefを指定していない引数に値を代入する、という謎のコードが書かれている。これじゃ意図がサッパリ読み取れない。 せめてソース内にコメント入れるなり、自分の意図した処理フローを文章で説明するなり、最低限質問するための努力はしようぜ。
Nanmotsu

2020/06/04 14:47

ご返答ありがとうございます。 ご指摘に合った通り、ソース内にコメント入れる等の配慮が足りませんでした。 エスパーしていただきました方の内容から「out」をつけることで解決しました。 勉強不足でした。みなさんありがとうございました。
guest

回答1

0

自己解決

「out」をつけることで解決しました。

C#

1public void grphicmethod(string name1, string name2, float buzzle, Vector3 point, out bool fit) 2 3/* メソッドの定義部分*/

C#

1gms.grphicmethod("tr", "trgoal", 0.2f, screenPoint, out trfit); 2 3/* メソッドの実行部分*/

投稿2020/06/04 14:50

Nanmotsu

総合スコア22

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問