teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

コードの追加

2021/01/04 23:51

投稿

shir
shir

スコア1

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,193 @@
10
10
 
11
11
  エラーとしてはParticleSystemオブジェクトが見つからないよ、といったことだと思うのですが、そもそもエフェクトをオブジェクトにただドラッグで追加しただけですからどこでエフェクトやプレイヤーオブジェクトの関係性が生まれたのかも全く分かりませんし、結局エラーの直し方もさっぱり分かりません。
12
12
 
13
- なぜこの様なエラーが出ているのでしょうか?そしてどうすればエラーが出なくなるのでしょうか?
13
+ なぜこの様なエラーが出ているのでしょうか?そしてどうすればエラーが出なくなるのでしょうか?
14
+
15
+ ###### エフェクトに付いているスクリプトEmitParticlesOnLand
16
+ ```html
17
+ using System.Collections;
18
+ using System.Collections.Generic;
19
+ using UnityEngine;
20
+ using System.Reflection;
21
+
22
+ [RequireComponent(typeof(ParticleSystem))]
23
+ public class EmitParticlesOnLand : MonoBehaviour
24
+ {
25
+
26
+ public bool emitOnLand = true;
27
+ public bool emitOnEnemyDeath = true;
28
+
29
+ #if UNITY_TEMPLATE_PLATFORMER
30
+
31
+ ParticleSystem p;
32
+
33
+ void Start()
34
+ {
35
+ p = GetComponent<ParticleSystem>();
36
+
37
+ if (emitOnLand) {
38
+ Platformer.Gameplay.PlayerLanded.OnExecute += PlayerLanded_OnExecute;
39
+ void PlayerLanded_OnExecute(Platformer.Gameplay.PlayerLanded obj) {
40
+ p.Play();
41
+ }
42
+ }
43
+
44
+ if (emitOnEnemyDeath) {
45
+ Platformer.Gameplay.EnemyDeath.OnExecute += EnemyDeath_OnExecute;
46
+ void EnemyDeath_OnExecute(Platformer.Gameplay.EnemyDeath obj) {
47
+ p.Play();
48
+ }
49
+ }
50
+
51
+ }
52
+
53
+ #endif
54
+
55
+ }
56
+
57
+ ```
58
+
59
+ ###### プレイヤーに付いているスクリプトPlayerController.cs
60
+ ```html
61
+ using System.Collections;
62
+ using System.Collections.Generic;
63
+ using UnityEngine;
64
+ using Platformer.Gameplay;
65
+ using static Platformer.Core.Simulation;
66
+ using Platformer.Model;
67
+ using Platformer.Core;
68
+
69
+ namespace Platformer.Mechanics
70
+ {
71
+ /// <summary>
72
+ /// This is the main class used to implement control of the player.
73
+ /// It is a superset of the AnimationController class, but is inlined to allow for any kind of customisation.
74
+ /// </summary>
75
+ public class PlayerController : KinematicObject
76
+ {
77
+ public AudioClip jumpAudio;
78
+ public AudioClip respawnAudio;
79
+ public AudioClip ouchAudio;
80
+
81
+ /// <summary>
82
+ /// Max horizontal speed of the player.
83
+ /// </summary>
84
+ public float maxSpeed = 7;
85
+ /// <summary>
86
+ /// Initial jump velocity at the start of a jump.
87
+ /// </summary>
88
+ public float jumpTakeOffSpeed = 7;
89
+
90
+ public JumpState jumpState = JumpState.Grounded;
91
+ private bool stopJump;
92
+ /*internal new*/ public Collider2D collider2d;
93
+ /*internal new*/ public AudioSource audioSource;
94
+ public Health health;
95
+ public bool controlEnabled = true;
96
+
97
+ bool jump;
98
+ Vector2 move;
99
+ SpriteRenderer spriteRenderer;
100
+ internal Animator animator;
101
+ readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>();
102
+
103
+ public Bounds Bounds => collider2d.bounds;
104
+
105
+ void Awake()
106
+ {
107
+ health = GetComponent<Health>();
108
+ audioSource = GetComponent<AudioSource>();
109
+ collider2d = GetComponent<Collider2D>();
110
+ spriteRenderer = GetComponent<SpriteRenderer>();
111
+ animator = GetComponent<Animator>();
112
+ }
113
+
114
+ protected override void Update()
115
+ {
116
+ if (controlEnabled)
117
+ {
118
+ move.x = Input.GetAxis("Horizontal");
119
+ if (jumpState == JumpState.Grounded && Input.GetButtonDown("Jump"))
120
+ jumpState = JumpState.PrepareToJump;
121
+ else if (Input.GetButtonUp("Jump"))
122
+ {
123
+ stopJump = true;
124
+ Schedule<PlayerStopJump>().player = this;
125
+ }
126
+ }
127
+ else
128
+ {
129
+ move.x = 0;
130
+ }
131
+ UpdateJumpState();
132
+ base.Update();
133
+ }
134
+
135
+ void UpdateJumpState()
136
+ {
137
+ jump = false;
138
+ switch (jumpState)
139
+ {
140
+ case JumpState.PrepareToJump:
141
+ jumpState = JumpState.Jumping;
142
+ jump = true;
143
+ stopJump = false;
144
+ break;
145
+ case JumpState.Jumping:
146
+ if (!IsGrounded)
147
+ {
148
+ Schedule<PlayerJumped>().player = this;
149
+ jumpState = JumpState.InFlight;
150
+ }
151
+ break;
152
+ case JumpState.InFlight:
153
+ if (IsGrounded)
154
+ {
155
+ Schedule<PlayerLanded>().player = this;
156
+ jumpState = JumpState.Landed;
157
+ }
158
+ break;
159
+ case JumpState.Landed:
160
+ jumpState = JumpState.Grounded;
161
+ break;
162
+ }
163
+ }
164
+
165
+ protected override void ComputeVelocity()
166
+ {
167
+ if (jump && IsGrounded)
168
+ {
169
+ velocity.y = jumpTakeOffSpeed * model.jumpModifier;
170
+ jump = false;
171
+ }
172
+ else if (stopJump)
173
+ {
174
+ stopJump = false;
175
+ if (velocity.y > 0)
176
+ {
177
+ velocity.y = velocity.y * model.jumpDeceleration;
178
+ }
179
+ }
180
+
181
+ if (move.x > 0.01f)
182
+ spriteRenderer.flipX = false;
183
+ else if (move.x < -0.01f)
184
+ spriteRenderer.flipX = true;
185
+
186
+ animator.SetBool("grounded", IsGrounded);
187
+ animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
188
+
189
+ targetVelocity = move * maxSpeed;
190
+ }
191
+
192
+ public enum JumpState
193
+ {
194
+ Grounded,
195
+ PrepareToJump,
196
+ Jumping,
197
+ InFlight,
198
+ Landed
199
+ }
200
+ }
201
+ }
202
+ ```

2

エラーコードをコードの表示内に

2021/01/04 23:51

投稿

shir
shir

スコア1

title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,11 @@
2
2
  [https://youtu.be/kNBWKuk_KYY?t=210](https://youtu.be/kNBWKuk_KYY?t=210)を見て実践しています。
3
3
  指定した時間のあたりですが、チュートリアルにあるエフェクトをプレイヤーオブジェクトに追加すると問題なく動作します。
4
4
 
5
- しかし動画の方では3:48あたりでエフェクトを削除している時動画が編集でカットされているのでよく分からないのですが、自分の方で同じように削除したところMissingReferenceException The object of type ParticleSystem has been destroyed but you are still trying to access it.というエラーが出てその後に同じエフェクトを追加し直しても全く動作しなくなってしまいます。
5
+ しかし動画の方では3:48あたりでエフェクトを削除している時動画が編集でカットされているのでよく分からないのですが、自分の方で同じように削除したところ
6
+ ```html
7
+ MissingReferenceException The object of type ParticleSystem has been destroyed but you are still trying to access it.
8
+ ```
9
+ というエラーが出てその後に同じエフェクトを追加し直しても全く動作しなくなってしまいます。
6
10
 
7
11
  エラーとしてはParticleSystemオブジェクトが見つからないよ、といったことだと思うのですが、そもそもエフェクトをオブジェクトにただドラッグで追加しただけですからどこでエフェクトやプレイヤーオブジェクトの関係性が生まれたのかも全く分かりませんし、結局エラーの直し方もさっぱり分かりません。
8
12
 

1

リンクの追加

2021/01/04 20:19

投稿

shir
shir

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,5 @@
1
1
  現在Unityさんの公式動画
2
- https://youtu.be/kNBWKuk_KYY?t=210
2
+ [https://youtu.be/kNBWKuk_KYY?t=210](https://youtu.be/kNBWKuk_KYY?t=210)を見て実践しています。
3
- を見て実践しています。
4
3
  指定した時間のあたりですが、チュートリアルにあるエフェクトをプレイヤーオブジェクトに追加すると問題なく動作します。
5
4
 
6
5
  しかし動画の方では3:48あたりでエフェクトを削除している時動画が編集でカットされているのでよく分からないのですが、自分の方で同じように削除したところMissingReferenceException The object of type ParticleSystem has been destroyed but you are still trying to access it.というエラーが出てその後に同じエフェクトを追加し直しても全く動作しなくなってしまいます。