回答編集履歴

1

senderを使うにはCubeScriptへキャスト

2016/08/21 13:37

投稿

narudesign
narudesign

スコア32

test CHANGED
@@ -149,3 +149,91 @@
149
149
  }
150
150
 
151
151
  ```
152
+
153
+
154
+
155
+ senderを使う場合はCubeScriptにキャストすれば大丈夫だと思います。
156
+
157
+
158
+
159
+ ```C#
160
+
161
+ using UnityEngine;
162
+
163
+ using System;
164
+
165
+
166
+
167
+ public class GameObjectScript : MonoBehaviour
168
+
169
+ {
170
+
171
+ CubeScript cubeScript;
172
+
173
+
174
+
175
+ void Awake()
176
+
177
+ {
178
+
179
+ // コンポーネント(CubeScript)を取得
180
+
181
+ cubeScript = GameObject.Find("Cube").GetComponent<CubeScript>();
182
+
183
+
184
+
185
+ // イベントを設定
186
+
187
+ cubeScript.OnCollisionEnterEvent += delegate (object sender, EventArgs e)
188
+
189
+ {
190
+
191
+ // キャストしたものを変数に
192
+
193
+ CubeScript cube = sender as CubeScript;
194
+
195
+ cube.transform.localPosition = new Vector3(0, 3, 0);
196
+
197
+
198
+
199
+ // または直接キャスト
200
+
201
+ // ((CubeScript)sender).transform.localPosition = new Vector3(0, 2, 0);
202
+
203
+
204
+
205
+ // メソッド呼び出し
206
+
207
+ MyOnCollisionEnter();
208
+
209
+ };
210
+
211
+
212
+
213
+ // イベントを設定
214
+
215
+ cubeScript.OnCollisionEnterEventWithArgs += delegate (object sender, EventArgsCollision e)
216
+
217
+ {
218
+
219
+ // 直接記述
220
+
221
+ Debug.Log("MyCollisionEnter " + e.col.transform.name);
222
+
223
+ };
224
+
225
+ }
226
+
227
+
228
+
229
+ void MyOnCollisionEnter()
230
+
231
+ {
232
+
233
+ Debug.Log("MyCollisionEnter");
234
+
235
+ }
236
+
237
+ }
238
+
239
+ ```