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

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

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

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

Visual Studio

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

Unity

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

Q&A

解決済

1回答

316閲覧

【Unity&C#】別のページにも同じテキストを反映する方法

k2zu1112

総合スコア18

C#

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

Visual Studio

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

Unity

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

0グッド

0クリップ

投稿2019/01/06 12:06

編集2019/01/08 00:32

最終目標

画像1枚目のGOALテキストと画像2枚目のGOALテキストの連動
イメージ説明>
イメージ説明

現状

1枚目のGOALテキストはスコアが上がる度に数値も上がっていくが、2枚目は反応がない(GOALの字はUI→Textとして書き込んでいる初期設定のまま)
イメージ説明

### 理想的な解決方法(他の方法でも解決すれば満足です)
1枚目のテキストボックスの内容(変化があった際も)を、そっくりそのまま別のテキストボックスにも反映してくれるようなスクリプトの構築。
または、複数ページにおいても同じ動きをする複数のテキストボックスの製作。

補足

・画像1枚目右下の「Shop」ボタンを押すことで、画像2枚目がPrefabsを使ったインスタンスとして出現(その間1枚目も存在はしているが、2枚目が上(手前)にあるために1枚目は全く見えない)
・2枚目のテキストに数値を反映することが出来れば、その間は1枚目のテキストは消えていても良い
・3枚目もある(2枚目とほぼ同じ)のでそちらにも使いたい

下に現在のスクリプトを載せています。
若干見にくいですが、必要なところを消してしまわないように全文載せています。
有識者の方、どうぞよろしくお願いします。

C#

1ng System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5using UnityEngine.UI; 6using System; 7 8public class GameManager : MonoBehaviour { 9 10 //定数定義 11 private const int MAX_ORB = 1000; //オーブ最大数 12 private const int RESPAWN_TIME = 120; //オーブが発生する秒数 13 private const int MAX_LEVEL = 5; //最大キーパーレベル 14 15 //データセーブ用キー 16 private const string KEY_SCORE = "SCORE"; //スコア 17 private const string KEY_LEVEL = "LEVEL"; //レベル 18 private const string KEY_ORB = "ORB"; //オーブ数 19 private const string KEY_TIME = "TIME"; //時間 20 private const string KEY_ITEM = "ITEM"; //アイテム 21 22 //オブジェクト参照 23 public GameObject orbPrefab; //オーブプレハブ 24 public GameObject trophyPrefab; //トロフィープレハブ 25 public GameObject canvasGame; //ゲームキャンバス 26 public GameObject textScore; //スコアテキスト 27 public GameObject imageKeeper; //キーパー 28 public float clickrate = 0.5f; 29 public GameObject ball; 30 public List<int> prices; 31 32 //メンバ変数 33 public int score = 0; //現在のスコア 34 //private int nextScore = 100; //レベルアップまでに必要なスコア 35 36 private int currentOrb = 0; //現在のオーブ数 37 38 private int keeperLevel = 0; //キーパーのレベル 39 40 private DateTime lastDateTime; //前回オーブを生成した瞬間 41 42 //private int[] nextScoreTable = new int[] { 100,200,300,400,500 }; //レベルアップ値 43 44 private int numOfOrb; //まとめて生成するオーブの数 45 46 // Use this for initialization 47 void Start () { 48 49 //データ削除メソッド 50 //PlayerPrefs.DeleteAll(); 51 52 //初期設定 53 score = PlayerPrefs.GetInt("SCORE", 0); 54 //keeperLevel = PlayerPrefs.GetInt(KEY_LEVEL, 0); 55 56 //nextScore = nextScoreTable[keeperLevel]; 57 //imageKeeper.GetComponent<KeeperManager>().SetKeeperPicture(keeperLevel); 58 //imageKeeper.GetComponent<KeeperManager>().SetKeeperScale(score, nextScore); 59 60 RefreshScoreText(); 61 62 } 63 64 // Update is called once per frame 65 void Update () { 66 //まとめて生成するオーブがあれば生成 67 while (numOfOrb > 0) 68 { 69 Invoke("CreateNewOrb",0.1f*numOfOrb); 70 numOfOrb--; 71 } 72 } 73 74 //バックグラウンドへの移行時と復帰時(アプリ起動時も含む)に呼び出される 75 76 private void OnApplicationPause(bool pauseStatus) 77 { 78 if (pauseStatus) 79 { 80 //アプリがバックグラウンドへ移行 81 } 82 else 83 { 84 //バックグラウンドから復帰 85 //時間の復元 86 string time = PlayerPrefs.GetString(KEY_TIME, ""); 87 if (time == "") 88 { 89 lastDateTime = DateTime.UtcNow; 90 } 91 else 92 { 93 long temp = Convert.ToInt64(time); 94 lastDateTime = DateTime.FromBinary(temp); 95 } 96 97 numOfOrb = 0; 98 //時間によるオーブ自動生成 99 TimeSpan timeSpan = DateTime.UtcNow - lastDateTime; 100 if (timeSpan >= TimeSpan.FromSeconds(RESPAWN_TIME)) 101 { 102 while (timeSpan >= TimeSpan.FromSeconds(RESPAWN_TIME)) 103 { 104 if (numOfOrb < MAX_ORB) 105 { 106 numOfOrb++; 107 } 108 timeSpan -= TimeSpan.FromSeconds(RESPAWN_TIME); 109 } 110 } 111 } 112 } 113 114 //新しいオーブの生成 115 public void CreateNewOrb() 116 { 117 lastDateTime = DateTime.UtcNow; 118 if (currentOrb >= MAX_ORB) 119 { 120 return; 121 } 122 CreateOrb(); 123 currentOrb++; 124 125 SaveGameData(); 126 } 127 128 //オーブ生成 129 public void CreateOrb() 130 { 131 GameObject orb = (GameObject)Instantiate(orbPrefab); 132 orb.transform.SetParent(canvasGame.transform, false); 133 orb.transform.localPosition = new Vector3( 134 UnityEngine.Random.Range (-100.0f, 100.0f),//オーブの出発点(横x) 135 UnityEngine.Random.Range (-300.0f, -450.0f), //オーブの出発点(縦y) 136 0f); 137 138 orb.GetComponent<OrbManager>().Flyorb(); 139 140 //ボールアニメーション 141 StartCoroutine("ClickAnim");//"ClickAnim"のコルーチンを呼ぶ. 142 143 } 144 145 146 147 //オーブ入手 148 public void GetOrb() 149 { 150 score += 1; 151 //キーパー画像切り替えメソッド挿入予定 152 153 /*//レベルアップ値を超えないように制限 154 if (score > nextScore) 155 { 156 score = nextScore; 157 } 158 159 KeeperLevelUp();*/ 160 RefreshScoreText(); 161 /*imageKeeper.GetComponent<KeeperManager>().SetKeeperScale(score, nextScore); 162 SaveGameData(); 163 164 //ゲームクリア判定 165 if ((score == nextScore) && (keeperLevel == MAX_LEVEL)) 166 { 167 //ClearEffect(); 168 } 169 currentOrb--; 170 */ 171 172 } 173 174 //スコアテキスト更新 175 void RefreshScoreText() 176 { 177 textScore.GetComponent<Text>().text = 178 score + "GOAL"; 179 } 180 181 /* //キーパーのレベル管理 182 void KeeperLevelUp() 183 { 184 if (score >= nextScore) 185 { 186 if (keeperLevel < MAX_LEVEL) 187 { 188 keeperLevel++; 189 score = 0; 190 191 KeeperLevelUpEffect(); 192 193 //nextScore = nextScoreTable[keeperLevel]; 194 imageKeeper.GetComponent<KeeperManager>().SetKeeperPicture(keeperLevel); 195 } 196 } 197 } 198 199 //レベルアップ時の演出 200 void KeeperLevelUpEffect() 201 { 202 GameObject trophy = (GameObject)Instantiate(trophyPrefab); 203 trophy.transform.SetParent(canvasGame.transform, false); 204 trophy.transform.SetSiblingIndex(5); 205 206 Destroy(trophy, 0.5f); 207 }*/ 208 //寺が最高レベル 209 /*void ClearEffect() 210 { 211 GameObject ??= (GameObject)Instantiate(??); 212 ??.transform.SetParent(canvasGame.transform, false); 213 }*/ 214 215 //ゲームデータをセーブ 216 void SaveGameData() 217 { 218 PlayerPrefs.SetInt("SCORE", score); 219 PlayerPrefs.SetInt(KEY_LEVEL,keeperLevel); 220 PlayerPrefs.SetInt(KEY_ORB,currentOrb); 221 PlayerPrefs.SetString(KEY_TIME,lastDateTime.ToBinary().ToString()); 222 223 224 PlayerPrefs.Save(); 225 } 226 227 //ボールサイズ編集 228 IEnumerator ClickAnim() 229 { 230 float time = 0;//アニメーションの時間を記録. 231 float scale = 1;//クッキーサイズ. 232 while (time < clickrate / 2) 233 {//アニメーションの半分の時間を拡大に使う. 234 time += Time.fixedDeltaTime;//時間をフレームの増分足す. 235 scale = 1 + time / clickrate / 8; 236 ball.transform.localScale = new Vector2(scale, scale);//クッキーのサイズを変える. 237 yield return new WaitForSeconds(Time.deltaTime);//フレームの終わりまで待つ. 238 } 239 //上の逆バージョン(今度は縮小処理) 240 while (time < clickrate / 2) 241 { 242 time += Time.fixedDeltaTime; 243 scale -= time / clickrate / 8; 244 ball.transform.localScale = new Vector2(scale, scale); 245 yield return new WaitForSeconds(Time.deltaTime); 246 } 247 ball.transform.localScale = new Vector2(1, 1);//スケールに誤差があるかもしれないので、ここで(1,1)に戻す. 248 }

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

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

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

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

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

guest

回答1

0

ベストアンサー

ショップメニューを開く瞬間に、該当するテキストボックスの内容をコピーすればいいと思います

csharp

1//ショップメニューのプレハブに取り付けて、Textに書き換えたいオブジェクトをセット 2class ShopText:MonoBehaviour { 3 public Text text; 4} 5 6 7// ショップメニューを開くところ 8GameObject shop = (GameObject)Instantiate(shopprefab); 9Text text = shop.GetComponent<ShopText>().text // 書き換えたいテキストのオブジェクトを取得 10text.text = textScore.GetComponent<Text>().text;

投稿2019/01/07 02:29

izmktr

総合スコア2856

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

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

k2zu1112

2019/01/08 00:39

izmktr様、ご回答ありがとうございます。 スクリプト内のネームだけ変更して使わせてもらっているのですが、 //ショップメニューを開くところ から2段下の<ShopText>の部分にどうしてもエラーが出てしまいます。(現状の項目にスクショを掲載しています。) Unity側ではエラーコード CS0118:`CreatePopupWindow.TextScore1' is a `field' but a `type' was expected と出ているのですが、どのようにすれば解決しますでしょうか? お手数ですが、お答えいただけると嬉しいです。
izmktr

2019/01/08 01:25

TextScore1って変数名ですよね、そこにはクラス名を指定します。
k2zu1112

2019/01/08 11:23

ご指摘ありがとうございます。 無事にエラーが解消されました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問