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

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

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

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

Q&A

0回答

1100閲覧

追加しただけのエフェクトを削除するとMissingReferenceExceptionエラーが出る

shir

総合スコア1

Unity

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

0グッド

0クリップ

投稿2021/01/04 20:09

編集2021/01/04 23:51

現在Unityさんの公式動画
https://youtu.be/kNBWKuk_KYY?t=210を見て実践しています。
指定した時間のあたりですが、チュートリアルにあるエフェクトをプレイヤーオブジェクトに追加すると問題なく動作します。

しかし動画の方では3:48あたりでエフェクトを削除している時動画が編集でカットされているのでよく分からないのですが、自分の方で同じように削除したところ

html

1MissingReferenceException The object of type ParticleSystem has been destroyed but you are still trying to access it.

というエラーが出てその後に同じエフェクトを追加し直しても全く動作しなくなってしまいます。

エラーとしてはParticleSystemオブジェクトが見つからないよ、といったことだと思うのですが、そもそもエフェクトをオブジェクトにただドラッグで追加しただけですからどこでエフェクトやプレイヤーオブジェクトの関係性が生まれたのかも全く分かりませんし、結局エラーの直し方もさっぱり分かりません。

なぜこの様なエラーが出ているのでしょうか?そしてどうすればエラーが出なくなるのでしょうか?

エフェクトに付いているスクリプトEmitParticlesOnLand

html

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Reflection; 5 6[RequireComponent(typeof(ParticleSystem))] 7public class EmitParticlesOnLand : MonoBehaviour 8{ 9 10 public bool emitOnLand = true; 11 public bool emitOnEnemyDeath = true; 12 13#if UNITY_TEMPLATE_PLATFORMER 14 15 ParticleSystem p; 16 17 void Start() 18 { 19 p = GetComponent<ParticleSystem>(); 20 21 if (emitOnLand) { 22 Platformer.Gameplay.PlayerLanded.OnExecute += PlayerLanded_OnExecute; 23 void PlayerLanded_OnExecute(Platformer.Gameplay.PlayerLanded obj) { 24 p.Play(); 25 } 26 } 27 28 if (emitOnEnemyDeath) { 29 Platformer.Gameplay.EnemyDeath.OnExecute += EnemyDeath_OnExecute; 30 void EnemyDeath_OnExecute(Platformer.Gameplay.EnemyDeath obj) { 31 p.Play(); 32 } 33 } 34 35 } 36 37#endif 38 39} 40
プレイヤーに付いているスクリプトPlayerController.cs

html

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Platformer.Gameplay; 5using static Platformer.Core.Simulation; 6using Platformer.Model; 7using Platformer.Core; 8 9namespace Platformer.Mechanics 10{ 11 /// <summary> 12 /// This is the main class used to implement control of the player. 13 /// It is a superset of the AnimationController class, but is inlined to allow for any kind of customisation. 14 /// </summary> 15 public class PlayerController : KinematicObject 16 { 17 public AudioClip jumpAudio; 18 public AudioClip respawnAudio; 19 public AudioClip ouchAudio; 20 21 /// <summary> 22 /// Max horizontal speed of the player. 23 /// </summary> 24 public float maxSpeed = 7; 25 /// <summary> 26 /// Initial jump velocity at the start of a jump. 27 /// </summary> 28 public float jumpTakeOffSpeed = 7; 29 30 public JumpState jumpState = JumpState.Grounded; 31 private bool stopJump; 32 /*internal new*/ public Collider2D collider2d; 33 /*internal new*/ public AudioSource audioSource; 34 public Health health; 35 public bool controlEnabled = true; 36 37 bool jump; 38 Vector2 move; 39 SpriteRenderer spriteRenderer; 40 internal Animator animator; 41 readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>(); 42 43 public Bounds Bounds => collider2d.bounds; 44 45 void Awake() 46 { 47 health = GetComponent<Health>(); 48 audioSource = GetComponent<AudioSource>(); 49 collider2d = GetComponent<Collider2D>(); 50 spriteRenderer = GetComponent<SpriteRenderer>(); 51 animator = GetComponent<Animator>(); 52 } 53 54 protected override void Update() 55 { 56 if (controlEnabled) 57 { 58 move.x = Input.GetAxis("Horizontal"); 59 if (jumpState == JumpState.Grounded && Input.GetButtonDown("Jump")) 60 jumpState = JumpState.PrepareToJump; 61 else if (Input.GetButtonUp("Jump")) 62 { 63 stopJump = true; 64 Schedule<PlayerStopJump>().player = this; 65 } 66 } 67 else 68 { 69 move.x = 0; 70 } 71 UpdateJumpState(); 72 base.Update(); 73 } 74 75 void UpdateJumpState() 76 { 77 jump = false; 78 switch (jumpState) 79 { 80 case JumpState.PrepareToJump: 81 jumpState = JumpState.Jumping; 82 jump = true; 83 stopJump = false; 84 break; 85 case JumpState.Jumping: 86 if (!IsGrounded) 87 { 88 Schedule<PlayerJumped>().player = this; 89 jumpState = JumpState.InFlight; 90 } 91 break; 92 case JumpState.InFlight: 93 if (IsGrounded) 94 { 95 Schedule<PlayerLanded>().player = this; 96 jumpState = JumpState.Landed; 97 } 98 break; 99 case JumpState.Landed: 100 jumpState = JumpState.Grounded; 101 break; 102 } 103 } 104 105 protected override void ComputeVelocity() 106 { 107 if (jump && IsGrounded) 108 { 109 velocity.y = jumpTakeOffSpeed * model.jumpModifier; 110 jump = false; 111 } 112 else if (stopJump) 113 { 114 stopJump = false; 115 if (velocity.y > 0) 116 { 117 velocity.y = velocity.y * model.jumpDeceleration; 118 } 119 } 120 121 if (move.x > 0.01f) 122 spriteRenderer.flipX = false; 123 else if (move.x < -0.01f) 124 spriteRenderer.flipX = true; 125 126 animator.SetBool("grounded", IsGrounded); 127 animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed); 128 129 targetVelocity = move * maxSpeed; 130 } 131 132 public enum JumpState 133 { 134 Grounded, 135 PrepareToJump, 136 Jumping, 137 InFlight, 138 Landed 139 } 140 } 141}

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

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

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

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

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

y_waiwai

2021/01/04 23:23

コードを提示しよう
shir

2021/01/04 23:58

ありがとうございます、コードを追加しました。 ちなみにチュートリアルのプロジェクトのままの状態ですので私自身はまだコードは書いておりません。 公式のプロジェクトのままですがエラーとなってしまうということです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問