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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Unity

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

Q&A

解決済

1回答

1386閲覧

3DUIを非表示にしてもシーン切り替えて戻ると表示される

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2019/12/25 08:29

現状

買うボタン押すと値段が消えるようにSetActiveをfalseにしている
シーン変えて戻ると値段がまた出ている

実現したいこと

値段の表示非表示でキャラ持っているかわかるようにしたい

コード

unity

1買うダイアログコード 2public class Buydialog : MonoBehaviour 3{ 4 public Button buy;//買うボタン 5 public Button back;//戻るボタン 6 public GameObject buydialog;//買うダイアログ 7 public GameObject gemdialog;//ジェムダイアログ 8 public GameObject[] needgem;//必要ジェムUI 9 // Start is called before the first frame update 10 void Start() 11 { 12 PlayerPrefs.GetInt("TotalGem");//トータルジェム取得 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 19 } 20 public void OnClick(int number) 21 { 22 switch (number) 23 { 24 case 0://買うボタン 25 if (PlayerPrefs.GetInt("BuyChara")==1)//キャラ1 26 { 27 PlayerPrefs.SetInt("Chara", 1);//スキンセット 28 PlayerPrefs.SetInt("Charactor1", 1);//キャラ所持 29 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30);//ジェム消費 30 buydialog.SetActive(false);//ダイアログ閉じる 31 gemdialog.SetActive(false);//ダイアログ閉じる 32 needgem[0].SetActive(false); 33 } 34 if (PlayerPrefs.GetInt("BuyChara") == 2)//キャラ2 35 { 36 PlayerPrefs.SetInt("Chara", 2); 37 PlayerPrefs.SetInt("Charactor2", 1); 38 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 39 buydialog.SetActive(false); 40 gemdialog.SetActive(false); 41 needgem[1].SetActive (false); 42 } 43 if (PlayerPrefs.GetInt("BuyChara") == 3)//キャラ3 44 { 45 PlayerPrefs.SetInt("Chara", 3); 46 PlayerPrefs.SetInt("Charactor3", 1); 47 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 48 buydialog.SetActive(false); 49 gemdialog.SetActive(false); 50 needgem[2].SetActive(false); 51 } 52 if (PlayerPrefs.GetInt("BuyChara") == 4)//キャラ4 53 { 54 PlayerPrefs.SetInt("Chara", 4); 55 PlayerPrefs.SetInt("Charactor4", 1); 56 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 57 buydialog.SetActive(false); 58 gemdialog.SetActive(false); 59 needgem[3].SetActive(false); 60 } 61 if (PlayerPrefs.GetInt("BuyChara") == 5)//キャラ5 62 { 63 PlayerPrefs.SetInt("Chara", 5); 64 PlayerPrefs.SetInt("Charactor5", 1); 65 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 66 buydialog.SetActive(false); 67 gemdialog.SetActive(false); 68 needgem[4].SetActive(false); 69 } 70 if (PlayerPrefs.GetInt("BuyChara") == 6)//キャラ6 71 { 72 PlayerPrefs.SetInt("Chara", 6); 73 PlayerPrefs.SetInt("Charactor6", 1); 74 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 60); 75 buydialog.SetActive(false); 76 gemdialog.SetActive(false); 77 needgem[5].SetActive(false); 78 } 79 break; 80 case 1://閉じるボタン 81 buydialog.SetActive(false); 82 break; 83 } 84 } 85}

unity

1キャラ選択コード 2public class Charaselect : MonoBehaviour 3{ 4 public GameObject[] charactor;//キャラ配列 5 public GameObject gemdialog;//ジェムダイアログ 6 public GameObject buydialog;//買うダイアログ 7 8 // Start is called before the first frame update 9 void Start() 10 { 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 18 } 19 20 public void OnClick(int number) 21 { 22 switch (number)//キャラ 23 { 24 case 0://キャラ0セット 25 PlayerPrefs.SetInt("Chara", 0); 26 //タイトルシーン読み込み 27 SceneManager.LoadScene("Title"); 28 break; 29 30 31 case 1://キャラ1 32 if (PlayerPrefs.GetInt("Charactor1") == 1)//キャラ1を所持していればセット 33 { 34 PlayerPrefs.SetInt("Chara", 1); 35 //タイトルシーン読み込み 36 SceneManager.LoadScene("Title"); 37 } 38 if (PlayerPrefs.GetInt("TotalGem") >= 30)//キャラ1を所持していなくて30ジェム以上所持 39 { 40 PlayerPrefs.SetInt("BuyChara", 1);//買うキャラを1に設定 41 buydialog.SetActive(true);//買うダイアログ出す 42 } 43 if (PlayerPrefs.GetInt("Charactor1") == 0)//キャラ1を持ってなくジェムもなければ 44 { 45 gemdialog.SetActive(true);//ジェムダイアログ表示 46 return; 47 } 48 break; 49 50 51 case 2://キャラ2 52 if (PlayerPrefs.GetInt("Charactor2") == 1) 53 { 54 PlayerPrefs.SetInt("Chara", 2); 55 56 //タイトルシーン読み込み 57 SceneManager.LoadScene("Title"); 58 } 59 if (PlayerPrefs.GetInt("TotalGem") >= 30) 60 { 61 PlayerPrefs.SetInt("BuyChara", 2); 62 buydialog.SetActive(true); 63 } 64 if (PlayerPrefs.GetInt("Charactor2") == 0) 65 { 66 gemdialog.SetActive(true); 67 return; 68 } 69 break; 70 71 72 case 3://キャラ3 73 if (PlayerPrefs.GetInt("Charactor3") == 1) 74 { 75 PlayerPrefs.SetInt("Chara", 3); 76 77 //タイトルシーン読み込み 78 SceneManager.LoadScene("Title"); 79 } 80 if (PlayerPrefs.GetInt("TotalGem") >= 30) 81 { 82 PlayerPrefs.SetInt("BuyChara", 3); 83 buydialog.SetActive(true); 84 } 85 if (PlayerPrefs.GetInt("Charactor3") == 0) 86 { 87 gemdialog.SetActive(true); 88 return; 89 } 90 break; 91 92 93 case 4://キャラ4 94 if (PlayerPrefs.GetInt("Charactor4") == 1) 95 { 96 PlayerPrefs.SetInt("Chara", 4); 97 98 //タイトルシーン読み込み 99 SceneManager.LoadScene("Title"); 100 } 101 if (PlayerPrefs.GetInt("TotalGem") >= 30) 102 { 103 PlayerPrefs.SetInt("BuyChara", 4); 104 buydialog.SetActive(true); 105 } 106 if (PlayerPrefs.GetInt("Charactor4") == 0) 107 { 108 gemdialog.SetActive(true); 109 return; 110 } 111 break; 112 113 114 case 5://キャラ5 115 if (PlayerPrefs.GetInt("Charactor5") == 1) 116 { 117 PlayerPrefs.SetInt("Chara", 5); 118 119 //タイトルシーン読み込み 120 SceneManager.LoadScene("Title"); 121 } 122 if (PlayerPrefs.GetInt("TotalGem") >= 30) 123 { 124 PlayerPrefs.SetInt("BuyChara", 5); 125 buydialog.SetActive(true); 126 } 127 if (PlayerPrefs.GetInt("Charactor5") == 0) 128 { 129 gemdialog.SetActive(true); 130 return; 131 } 132 break; 133 134 135 case 6://キャラ6 136 if (PlayerPrefs.GetInt("Charactor6") == 1) 137 { 138 PlayerPrefs.SetInt("Chara", 6); 139 140 //タイトルシーン読み込み 141 SceneManager.LoadScene("Title"); 142 } 143 if (PlayerPrefs.GetInt("TotalGem") >= 60) 144 { 145 PlayerPrefs.SetInt("BuyChara", 6); 146 buydialog.SetActive(true); 147 } 148 if (PlayerPrefs.GetInt("Charactor6") == 0) 149 { 150 gemdialog.SetActive(true); 151 return; 152 } 153 break; 154 } 155 } 156} 157 158

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

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

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

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

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

guest

回答1

0

ベストアンサー

シーンが切り替わった場合、一度全てのオブジェクトが破棄されます。

なので「falseにした」という情報をどこかに保存する必要があります。
方法は沢山ありますが、キャラの保持との事なのでどこかにキャラの所持リストを作成し、該当キャラを所持している場合falseにするみたいな処理をStartメソッド等に書くみたいな方法が出来ると思います。

Scene切替時でもGameObject等を破棄せずに保持しておく方法

[Unity]シングルトンを使ってみよう

投稿2019/12/26 01:16

編集2019/12/26 03:12
simapiko

総合スコア332

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

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

退会済みユーザー

退会済みユーザー

2019/12/26 02:54

その場合スタート関数でif文を使ってキャラ一つ一つ書いていくということでしょうか?
simapiko

2019/12/26 03:12

必ずしもStartである必要は無いかもしれないですが、そのようになるかと思います。今のままだと冗長に見えるので、最良はメソッドに「持っていれば非表示にする処理」を纏められるといいのですが、難しい内は1つ1つでいいかと思います。 (PlayerPrefsで所持情報管理してましたね。、見落としてました。不必要な情報消しておきます。)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問