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

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

新規登録して質問してみよう
ただいま回答率
85.51%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

2863閲覧

MissingReferenceExceptionのエラーで困ってます

uunty

総合スコア9

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

1クリップ

投稿2018/06/29 09:35

編集2018/07/08 14:45

前提・実現したいこと

PlayerがEnemyの当たり判定に当たるとPlayerをデストロイさせたいんですが
MissingReferenceExceptionのエラーが出て途中で止まってしまいます。
ネットで調べたところNULLをする必要があるらしくどこに追加すればいいか分からなくて
変えてもエラーが出てしまいます。

該当のソースコード

search.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class search : MonoBehaviour { public bool playerClosing; private SphereCollider sphereCol; // Use this for initialization void Start() { sphereCol = GetComponent<SphereCollider>(); } // Update is called once per frame void OnTriggerStay(Collider col) { if (col.gameObject.tag == "Player") { playerClosing = true; sphereCol.radius = 10f; } } void OnTriggerExit(Collider col) { playerClosing = false; sphereCol.radius = 5f; } }
Enemy.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { private float speed = 350f; private float rotationSmooth = 1f; private Vector3 targetPosition; private float changeTargetSqrDistance = 40f; private Rigidbody rb; public bool playerclosing; public GameObject searching; public GameObject player; public float attackPoint = 10f; // Use this for initialization void Start() { //targetPosition = GetRandomPositionOnLevel(); rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { playerclosing = searching.GetComponent<search>().playerClosing; //現在地から目標地点までの距離を取得、短かったならもう一度目標地点を設定しなおす。 float sqrDistanceToTarget = Vector3.SqrMagnitude(transform.position - targetPosition); if (sqrDistanceToTarget < changeTargetSqrDistance) { targetPosition = GetRandomPositionOnLevel(); } //もし索敵範囲内にプレイヤーがいたら目標地点をプレイヤーのいる場所に変更する if (playerclosing) { targetPosition = player.transform.position; } //目標地点の方向を向く Quaternion TargetRotation = Quaternion.LookRotation(targetPosition - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, TargetRotation, Time.deltaTime * rotationSmooth); //前へ進む rb.velocity = (transform.forward * speed * Time.deltaTime); //Debug.DrawLine(transform.position,targetPosition,Color.red); } public Vector3 GetRandomPositionOnLevel() { float levelSize = 10f; return new Vector3(Random.Range(-levelSize, levelSize), 0, Random.Range(-levelSize, levelSize)); //ここで処理を終了(return)し、この関数を開始する直前の位置へ戻る } }
Attack.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Attack : MonoBehaviour { public bool playerAttak; private BoxCollider boxCol; public GameObject player; // Use this for initialization void Start() { boxCol = GetComponent<BoxCollider>(); player = GameObject.FindWithTag("Enemy"); } // Update is called once per frame void OnTriggerStay(Collider col) { if (col.gameObject.tag == "Player") { playerAttack = true; Destroy(gameObject); } } }
エラー内容 MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.GameObject.GetComponent[search] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:38) Enemy.Update () (at Assets/Enemy.cs:33)

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

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

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

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

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

guest

回答1

0

ベストアンサー

EnemyのUpdateの一行目じゃないでしょうか

playerclosing = searching.GetComponent<search>().playerClosing;
修正案
search serchComponent = seaching.GetComponent<search>(); if(search != null) { playerclosing = serchComponent.playerClosing; }

投稿2018/07/11 13:49

m_hikari

総合スコア64

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

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

uunty

2018/07/18 13:20

返事遅れて申し訳ございません if(search != null)と入力すると 'search' は '型' ですが、'変数' のように使用されています。 というエラーになってしまいました。
m_hikari

2018/07/18 13:42

すいません、入力ミスです。 if(serchComponent != null) ↑このように書くつもりでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問