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

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

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

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

Unity

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

Q&A

0回答

659閲覧

【Unity】Unity Learn Unit 2 - Basic Gameplayでオブジェクトが破壊された後呼び出せない

poyo123

総合スコア4

C#

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

Unity

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

0グッド

0クリップ

投稿2021/06/08 13:58

前提・実現したいこと

position(0,0,12)rotation(0,180,0)にある動物オブジェクトが破壊された後Sボタンを押して動物プレハブを出そうとしてもエラーが出る。

動物オブジェクトにはMoveForwordをアタッチしていているので一定のスピードで動き、DestroyOutOfBoundをアタッチしているのでposition.zが-10を超えると破壊される。

GameObjectにはSpawnManegerをアタッチしてAnimal Prefabsに動物オブジェクトのプレハブをアタッチしている。

発生している問題・エラーメッセージ

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. SpawnManeger.Update () (at Assets/Scripts/SpawnManeger.cs:21)

該当のソースコード

using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveForword : MonoBehaviour { public float speed = 40.0f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.Translate(Vector3.forward * Time.deltaTime * speed); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyOutOfBound : MonoBehaviour { private float topBound = 30; private float lowerBound = -10; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (transform.position.z > topBound) { Destroy(gameObject); } else if (transform.position.z < lowerBound) { Destroy(gameObject); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpawnManeger : MonoBehaviour { public GameObject[] animalPrefabs; public int animalIndex; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.S)) { Instantiate(animalPrefabs[0], new Vector3(0, 0, 20), animalPrefabs[animalIndex].transform.rotation); } }

試したこと

チュートリアル動画をある程度見返してみた

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

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

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

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

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

sakura_hana

2021/06/10 00:22

animalPrefabsにセットしているプレハブオブジェクトがシーン上に存在していませんか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問