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

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

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

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

Q&A

解決済

1回答

1082閲覧

キャラを買うのに一つのボタンで買うキャラを分けたい

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2019/12/23 16:10

編集2019/12/24 12:14

現状

ほしいキャラを押してコイン(アイテム)を持っていないとbuydaialogが出るようにしている
switch文を使って買う(buy)ボタンと閉じる(back)ボタンに0と1を設定している
現状一つ買うとすべて変えてしまいアイテムがマイナスになってしまう

実現したいこと

ほしいキャラを押して出たダイアログで買うを押すとそのキャラが買えるようにしたい

試したこと

switch分の中にswitch文やif文を作ってみたけどうまくいかなかった

該当スクリプト

unity

1買うダイアログコード 2 public Button buy; 3 public Button back; 4 public GameObject buydialog; 5 public GameObject[] charactor; 6 7 // Start is called before the first frame update 8 void Start() 9 { 10 PlayerPrefs.GetInt("TotalGem"); 11 } 12 13 // Update is called once per frame 14 void Update() 15 { 16 17 } 18 public void OnClick(int number) 19 { 20 switch (number) 21 { 22 case 0: 23 if (charactor[0]) 24 { 25 PlayerPrefs.SetInt("Chara", 1); 26 PlayerPrefs.SetInt("Charactor1", 1); 27 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 28 buydialog.SetActive(false); 29 } 30 if (charactor[1]) 31 { 32 PlayerPrefs.SetInt("Chara", 2); 33 PlayerPrefs.SetInt("Charactor2", 1); 34 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 35 buydialog.SetActive(false); 36 } 37 if (charactor[2]) 38 { 39 PlayerPrefs.SetInt("Chara", 3); 40 PlayerPrefs.SetInt("Charactor3", 1); 41 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 42 buydialog.SetActive(false); 43 } 44 break; 45 case 1: 46 buydialog.SetActive(false); 47 break; 48 } 49 } 50} 51

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 PlayerPrefs.GetInt("TotalGem"); 13 PlayerPrefs.GetInt("Chara"); 14 PlayerPrefs.GetInt("Charactor1"); 15 PlayerPrefs.GetInt("Charactor2"); 16 PlayerPrefs.GetInt("Charactor3"); 17 PlayerPrefs.GetInt("Charactor4"); 18/////////////////////////////////////////////////////////// 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 25 } 26 27 public void OnClick(int number) 28 { 29 switch (number) 30 { 31 case 0: 32 PlayerPrefs.SetInt("Chara", 0); 33 //タイトルシーン読み込み 34 SceneManager.LoadScene("Title"); 35 break; 36 37 38 case 1: 39 if (PlayerPrefs.GetInt("Charactor1") == 1) 40 { 41 PlayerPrefs.SetInt("Chara", 1); 42 43 //タイトルシーン読み込み 44 SceneManager.LoadScene("Title"); 45 } 46 47 if (PlayerPrefs.GetInt("TotalGem") >= 30) 48 { 49 buydialog.SetActive(true); 50 } 51 52 if (PlayerPrefs.GetInt("Charactor1") == 0) 53 { 54 gemdialog.SetActive(true); 55 return; 56 } 57 break; 58 59 case 2: 60 if (PlayerPrefs.GetInt("Charactor2") == 1) 61 { 62 PlayerPrefs.SetInt("Chara", 2); 63 64 //タイトルシーン読み込み 65 SceneManager.LoadScene("Title"); 66 } 67 68 if (PlayerPrefs.GetInt("TotalGem") >= 30) 69 { 70 buydialog.SetActive(true); 71 } 72 73 if (PlayerPrefs.GetInt("Charactor2") == 0) 74 { 75 gemdialog.SetActive(true); 76 return; 77 } 78 break; 79 80 case 3: 81 if (PlayerPrefs.GetInt("Charactor3") == 1) 82 { 83 PlayerPrefs.SetInt("Chara", 3); 84 85 //タイトルシーン読み込み 86 SceneManager.LoadScene("Title"); 87 } 88 89 if (PlayerPrefs.GetInt("TotalGem") >= 30) 90 { 91 PlayerPrefs.SetInt("Chara", 3); 92 PlayerPrefs.SetInt("Charactor3", 1); 93 //タイトルシーン読み込み 94 SceneManager.LoadScene("Title"); 95 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 96 } 97 98 if (PlayerPrefs.GetInt("Charactor3") == 0) 99 { 100 gemdialog.SetActive(true); 101 return; 102 } 103 break; 104 105 case 4: 106 if (PlayerPrefs.GetInt("Charactor4") == 1) 107 { 108 PlayerPrefs.SetInt("Chara", 4); 109 110 //タイトルシーン読み込み 111 SceneManager.LoadScene("Title"); 112 } 113 114 if (PlayerPrefs.GetInt("TotalGem") >= 30) 115 { 116 PlayerPrefs.SetInt("Chara", 4); 117 PlayerPrefs.SetInt("Charactor4", 1); 118 //タイトルシーン読み込み 119 SceneManager.LoadScene("Title"); 120 PlayerPrefs.SetInt("TotalGem", PlayerPrefs.GetInt("TotalGem") - 30); 121 } 122 123 if (PlayerPrefs.GetInt("Charactor4") == 0) 124 { 125 gemdialog.SetActive(true); 126 return; 127 } 128 break; 129 } 130 } 131}

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

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

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

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

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

U_U_Jenkins

2019/12/24 03:15

質問内容の「スクリプト」がなんのスクリプトを指すのかを記述するとわかりやすいと思いますよ あと、実現したいことだけでは「あなたは何がわからないのか」というのがこちら側に伝わらないので、回答者側は回答しにくいかもしれません。そのあたりの補足お願いします。
退会済みユーザー

退会済みユーザー

2019/12/24 03:30

ご指摘有難うございます 只今通信できる環境でないので今夜にでも作ってみたスクリプト(上手くいかなかった)と合わせて修正させて戴きます その際よろしければ返答お待ちしております
guest

回答1

0

ベストアンサー

ボタンにif文を作ってキャラ選択の方でplayerprefasを作りそれを条件にすることでできた

投稿2019/12/25 03:36

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問