###やりたいこと
"Player"が"Clock"というオブジェクトに触れると"Timer(残り時間)"に15秒加算することをしたいです。
細かく言うと、別のスプリクトの変数を変更したいです。
###コードとインスペクター
Timer
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Timer : MonoBehaviour 7{ 8 public int T; 9 public float time ; 10 void Update() 11 { 12 time -= Time.deltaTime;//1秒ごとに1ずつ引く 13 T = (int)time;//表示の時に小数点以下はいらないためint型に変換 14 Text t = GameObject.Find("Timer").GetComponent<Text>();//"Timer"を見つける 15 t.text = "Time:"+T;//表示 16 //省略 17 } 18}
※"時間"というのは"time"が勝手に翻訳されているだけです。
Player
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player : MonoBehaviour 6{ 7 GameObject timer; 8 //省略 9 void OnTriggerEnter2D(Collider2D other)//触れたときに実行 10 { 11 timer = GameObject.Find("Timer");//オブジェクトを見つける 12 Timer timer2 = timer.GetComponent<Timer>();//"Timer"のスプリクト(Timer)にアクセスする 13 if (other.gameObject.tag == "Clock")//当たったオブジェクトのタグが"Clock"ならば 14 { 15 timer2.T += 15;//15秒加算する 16 //↑ここに対してエラーが出ている 17 } 18 } 19}
※2つの画像の黒い線は気にしないでください。
###エラー
NullReferenceException: Object reference not set to an instance of an object
###試したこと
ネットで調べた、「Unityの再起動」と「インスペクターに何も表示されていない状態で実行する」という方法はやってみましたが、解決できませんでした...
###環境
Windows10 Pro
Unity 2020.3.13f1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。