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

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

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

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

Unity

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

Q&A

解決済

1回答

2066閲覧

unity: boolを使った条件分岐が反応しない

da-ryo

総合スコア11

C#

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

Unity

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

0グッド

0クリップ

投稿2021/03/22 14:22

編集2021/04/18 03:55

#現状
unity.VScodeにてスクリプトを書いております。

①UIのボタンを押す

②あるオブジェクトのboolをtrueにする。

③true後の処理を実行する

上記の流れを実現したいです!

#スクリプト

private bool sleep; public void SleepOn() { sleep = true; Debug.Log ("trueにしました"); } void Update() { Debug.Log (sleep); if (sleep == true) { Debug.Log ("ok");     } }

#コンソール画面
上記のスクリプト上の"trueにしました"は表記されるのですが、"ok"は表記されません。
Debug.Log (sleep);は常にFalseをフレームごとに表記していたためそもそもtrueにできていないようです。
#わからないこと
上記のコンソールから、ボタンを押したあとSleepOnメソッドは実行されているようですが、その中のsleep=trueがうまく機能してないようです。

それを機能させてコンソールにokを表示したいのですがどうすればよろしいでしょうか。

解決よろしくお願いします!

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

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

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

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

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

edo_m18

2021/03/22 14:42

コード全文だとどうなっていますか? ここだけを見るとうまく動くように見えますね。
da-ryo

2021/04/18 03:54 編集

返信遅れました! 独学なので汚くて長いスクリプトですが一応全文貼ります。 ``` using System.Collections; using System.Collections.Generic; using UnityEngine; public class BlueManager : MonoBehaviour { public float speed; private Animator animator; public float MaxHp; public GameObject ball; PlayerAttack script; private GameObject player; ExpManager expScript; private bool walk; float currentHp; [SerializeField] GameObject DamageFire; //アイテム public GameObject healRed; public GameObject healGreen; public GameObject healYellow; public GameObject healWhite; public GameObject powerUp; public GameObject coin; public GameObject fiveCoin; public GameObject bullian; Bank bankScript; public GameObject bomb; Bomb bombScript; private bool sleep; private float timeS; // Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); player = GameObject.Find ("PlayerBack"); script = player.GetComponent<PlayerAttack>(); expScript = player.GetComponent<ExpManager>(); bankScript = player.GetComponent<Bank>(); MaxHp = 700; currentHp = MaxHp; walk = true; sleep = false; bombScript = bomb.GetComponent<Bomb>(); } // Update is called once per frame void Update() { Debug.Log (sleep); //移動 if(walk == true) { Walking(); } //移動ストップ if (sleep == true) { Debug.Log ("ok"); speed = 0; timeS += Time.deltaTime; //15秒後移動を開始 if (timeS > 15) { speed = 0.3f; SleepOff(); } } //死んだ時 if (currentHp <= 0) { animator.SetBool( "IsDie", true); StartCoroutine("Death"); walk = false; } } //移動 public void Walking() { transform.position += new Vector3( 0, 0, -speed * Time.deltaTime); animator.SetBool( "IsWalk", true); } private void OnCollisionEnter(Collision other) { if (other.gameObject.tag == "Player") { bankScript.LossCoin(); Destroy (this.gameObject); } if (other.gameObject.tag == "Ball") { float damage = script.Attack; float lateHp = currentHp - damage; currentHp = lateHp; //Debug.Log(currentHp); Vector3 DamagePos = this.transform.position; Instantiate ( DamageFire, DamagePos, Quaternion.identity); } if (other.gameObject.tag == "Bomb") { int bombDamage = 30; currentHp = currentHp - bombDamage; Vector3 DamagePos = this.transform.position; Instantiate ( DamageFire, DamagePos, Quaternion.identity); Destroy (other.gameObject); } } //死んだ時のドロップ IEnumerator Death() { yield return new WaitForSeconds(1); int drop = Random.Range ( 1, 101); if (drop <= 30) { int dropH = Random.Range ( 1, 101); if (dropH >= 1 && drop <= 40) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( healWhite, itemPos, Quaternion.identity); } if (dropH >= 41 && dropH <= 70) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( healYellow, itemPos, Quaternion.identity); } if (dropH >= 71 && dropH <= 90) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( healGreen, itemPos, Quaternion.identity); } if (dropH >= 91 && dropH <= 100) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( healRed, itemPos, Quaternion.identity); } } if (drop >= 31 && drop <= 60) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( powerUp, itemPos, Quaternion.identity); } //コインドロップ if (drop >= 61 && drop <= 100) { int dropC = Random.Range ( 1, 101); if (dropC >= 1 && dropC <= 45) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( coin, itemPos, Quaternion.identity); } if (dropC >= 46 && dropC <= 80) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( fiveCoin, itemPos, Quaternion.identity); } if (dropC >= 81 && dropC <= 100) { Vector3 itemPos = new Vector3 ( this.transform.position.x, this.transform.position.y + 1, this.transform.position.z); Instantiate ( bullian, itemPos, Quaternion.identity); } } expScript.BlueExp(); Destroy(this.gameObject); } //ボタンが押されたら起動する関数 public void SleepOn() { //ここのブールも起動しない animator.SetBool( "IsSleep", true); sleep = true; } //ボタンの処理から元に戻る private void SleepOff() { sleep = false; timeS = 0; animator.SetBool( "IsSleep", false); walk = true; } }```
da-ryo

2021/03/25 05:42

追記:スクリプトの流れ ①画面上のボタンを押すと下から2番目のSleepOn()が起動 ②Update内のif (sleep == true)が始まる ③15秒後に一番下のSleepOff()が起動して元に戻る と言った流れです! 実行したい処理とは別の関数も書き込みましたが解決お願いします!
guest

回答1

0

ベストアンサー

いただいたコードを拝見しましたが、やはり正常に動くように見えます。
ちなみにエラーは出ていない前提なんですが、そこは問題ないですよね?

あと、実際に SleepOn を読んでいる対象と、実際に見ている対象が違うということはないですか?
(複数のGameObjectがあって、間違った対象の SleepOn を読んでるとかはないですか?)

投稿2021/03/25 07:19

edo_m18

総合スコア2283

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

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

da-ryo

2021/04/03 03:28

1週間ほどかけて全部のスクリプトとオブジェクトを見直してみたのですがやはりSleepOnが他にあるわけではなさそうです。Debug.Logで確認した通りSleepOn自体は読み込めているのですがその中のsleep = trueが読み込めていないみたいです。 もう少し試してみて無理そうなら今回この処理は諦めようと思います。 ここまで回答してくださりありがとうございました。
fiveHundred

2021/04/18 06:35

> 1週間ほどかけて全部のスクリプトとオブジェクトを見直してみたのですが IDE(というかVisualStudio)の検索機能を使えば、数秒かつ確実に出来ると思うのですが…。
da-ryo

2021/04/19 20:19

そのような機能があるんですね! ありがとうございます。あまりスクリプトを振り返る機会がなかったのですが助かります!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問