前提
ここに質問の内容を詳しく書いてください。
(例)
unityでFPSゲームを作っています。
弾丸の残りや装填数を表示するUIをテキストで作りました
しかしエラーになってしまいます。
実現したいこと
エラーを直したい。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object
bulletsUI.Update () (at Assets/bulletsUI.cs:21)
該当のソースコード
bulleUI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class bulletsUI : MonoBehaviour
{
public int magazineSize;
public int bullets;
private Text txt; // Start is called before the first frame update void Start() { txt = gameObject.GetComponent<Text>(); } // Update is called once per frame void Update() { txt.text = bullets + "/" + magazineSize; }
}
試したこと
問題となっているのが21行目の
txt.text = bullets + "/" + magazineSize;
と表示されており、それぞれ綴りや大文字小文字のチェックなどを行いました。
補足情報(FW/ツールのバージョンなど)
バージョンは
Unity 2021.3.7f1
です
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/07 08:44
2022/08/07 09:00
2022/08/07 09:32
2022/08/07 10:08
2022/08/07 11:04
2022/08/07 11:54
2022/08/07 12:01
2022/08/07 12:57