unity バージョン2021,2,15f1
windows10
unityで車(carオブジェクト)と旗(flagオブジェクト)の距離をui(Distance)でマイフレーム更新して画面に出力するはずですがエラーになってしまいます
エラー内容は以下になります
NullReferenceException: Object reference not set to an instance of an object GameDirector.update()(at Assets/GameDirector.cs:22)
unityのオブジェクトと名前は一致しています
|```c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
GameObject car;
GameObject flag;
GameObject distance;
void Start() { this.car = GameObject.Find("car"); this.flag = GameObject.Find("flag"); this.distance = GameObject.Find("Distance"); } // Update is called once per frame void Update() { float length = this.flag.transform.position.x - this.car.transform.position.x; this.distance.GetComponent<Text>().text = "ゴールまで" + length.ToString("F2") + "m"; }
}
回答3件
あなたの回答
tips
プレビュー