実現したいこと
- テキストを更新して、ゲームスコアやゲームクリア等の表示・変更したい。
- ▲▲機能を動作するようにする
前提
【Unity 入門】【チュートリアル】玉転がしゲームを作る 7. スコアとリザルトの UI の作成というサイトに従って、ゲームを制作していたが、テキストを変更する仕組みを実装するところで問題が発生。
具体的には、
「「Score Text」オブジェクトと、「Win Text」オブジェクトを
それぞれの項目にドラッグして離します」
という文章がある部分。
素人意見ですが、サイトと自分が使用しているUnityのバージョンが違うために起こった不具合なのではと思っている。
発生している問題・エラーメッセージ
テキストオブジェクトをドラッグ&ドロップすることができない。
ドラッグ&ドロップできるように、chatGPTを利用して、修正できたとしても、テキストが更新されない。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using Unity.VisualScripting; 4using UnityEngine.SocialPlatforms.Impl; 5using UnityEngine; 6using UnityEngine.UI; 7 8public class PlayerController : MonoBehaviour 9{ 10 11 public float speed = 20;//移動速度 12 public Text scoreText;//スコアのUI 13 public Text winText;//リザルトのUI 14 private Rigidbody rb;//Rigidbody 15 private int score;//スコア 16 17 // Start is called before the first frame update 18 void Start() 19 { 20 rb = GetComponent<Rigidbody>(); 21 22 //UIを初期化 23 score = 0; 24 SetCountText(); 25 winText.text = ""; 26 } 27 28 // Update is called once per frame 29 void Update() 30 { 31 //WASDキーの入力を取得 32 float moveHorizontal = Input.GetAxis("Horizontal"); 33 float moveVertical = Input.GetAxis("Vertical"); 34 35 //カーソルキーの入力に合わせて移動方向を設定 36 var movement = new Vector3(moveHorizontal, 0, moveVertical); 37 38 //Rigidbodyに力を加えて玉を動かす 39 rb.AddForce(movement * speed); 40 } 41 42 void OnTriggerEnter(Collider other) 43 { 44 //ぶつかったオブジェクトが収集アイテムだった場合 45 if(other.gameObject.CompareTag("Pick Up")) 46 { 47 other.gameObject.SetActive(false); 48 49 score = score + 1; 50 51 SetCountText(); 52 } 53 } 54 55 private void SetCountText() 56 { 57 58 scoreText.text = "Count: " + score.ToString(); 59 60 if(score >= 12) 61 { 62 winText.text = "You Win!"; 63 } 64 } 65} 66
試したこと
①UnityやVisualStudioの再起動や、PCの再起動を行ったが、問題は解決されず、進展なし。
② 番号リストchatGPTにスクリプト修正案を質問し、8行目から26行目を下記スクリプトのように修正し、Unity内でテキストを、ドロップはできたが、Unityで実行したところ、テキストは変更されなかった。
C#
1public class PlayerController : MonoBehaviour 2{ 3 public float speed; // 動く速さ 4 public GameObject scoreTextObject; // スコアのテキストオブジェクト 5 public GameObject winTextObject; // リザルトのテキストオブジェクト 6 7 private Rigidbody rb; // Rididbody 8 private int score; // スコア 9 private Text scoreText; // スコアテキストコンポーネント 10 private Text winText; // リザルトテキストコンポーネント 11 12 void Start() 13 { 14 // Rigidbody を取得 15 rb = GetComponent<Rigidbody>(); 16 17 // UI のテキストコンポーネントを取得 18 scoreText = scoreTextObject.GetComponent<Text>(); 19 winText = winTextObject.GetComponent<Text>(); 20 21 // UI を初期化 22 score = 0; 23 SetCountText(); 24 winText.text = ""; 25 }
③もう一度、修正前のスクリプトに戻して、実行したが、ドロップができず、実装できなかった。
補足情報(FW/ツールのバージョンなど)
Unity 2021.3.22f1
Visual Studio 2022
参考文献:【Unity 入門】【チュートリアル】玉転がしゲームを作る 7. スコアとリザルトの UI の作成

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。