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

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

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

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

Q&A

解決済

1回答

3172閲覧

NullReferenceExceptionのエラー

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2016/10/12 02:33

NullReferenceException: Object reference not set to an instance of an object
EvasiveManeuver.Start () (at Assets/Script/EvasiveManeuver.cs:22)
とエラーが出ました。

using UnityEngine; using System.Collections; public class EvasiveManeuver : MonoBehaviour { public float dodge; public float smoothing; public float tilt; public Vector2 startWait; public Vector2 maneuverTime; public Vector2 maneuverWait; public Boundary boundary; public Transform playerTransform; private float currentSpeed; private float targetManeuver; private Rigidbody rb; // Use this for initialization void Start () { rb = GetComponent <Rigidbody> (); playerTransform = GameObject.FindGameObjectWithTag ("Player").transform; currentSpeed = rb.velocity.z; StartCoroutine (Evade ()); } IEnumerator Evade() { yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y)); while (true) { targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x); yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y)); targetManeuver = 0; yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y)); } } void FixedUpdate () { float newManeuver = Mathf.MoveTowards (rb.velocity.x, targetManeuver, Time.deltaTime * smoothing); rb.velocity = new Vector3 (newManeuver, 0.0f, currentSpeed); rb.position = new Vector3( Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax) ); rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt); } }

とコードを書き

playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;

の部分がおかしいと指摘されました。

これはunityのインスペクタ側に必要なものがないエラーなのでしょうか、
それともソースコードに必要なものがないのでしょうか?
どちらに何を付け加えたら良いのでしょうか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

Playerというタグが付いているオブジェクトがシーン上に存在しないのが原因です。

GameObject.FindGameObjectWithTag ("Player") はPlayerというタグの付いたオブジェクトを探すが、見付からない場合nullが入る。

playerTransform = null.transform; となるが、nullの中にはtransformが存在しないのでエラーを吐く。

投稿2016/10/12 07:59

sakura_hana

総合スコア11427

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問