[英]Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject'
I am following a tutorial for a 2D platformer and ran into this error that I have not been able to find a solution for yet.我正在关注 2D 平台游戏的教程,但遇到了这个我还没有找到解决方案的错误。 I'm new to code and GameDev.我是代码和 GameDev 的新手。
I have tried changing the type under the variable declaration from Transform to GameObject which clears the compile error but then throws a new error within Unity "InvalidCastException.我尝试将变量声明下的类型从 Transform 更改为 GameObject 以清除编译错误,但随后在 Unity “InvalidCastException.
Sorry if this is such a simple fix, this is all new to me.对不起,如果这是一个如此简单的修复,这对我来说是全新的。
public void _KillEnemy(Enemy _enemy)
{
GameObject _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as GameObject;
Destroy(_clone, 5f);
cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
Destroy(_enemy.gameObject);
}
You can't cast a Transform
to a GameObject
.您不能将Transform
为GameObject
。 You need to call the property gameObject
of Transform
.您需要调用Transform
的属性gameObject
。 See more: https://docs.unity3d.com/ScriptReference/Component-gameObject.html查看更多: https://docs.unity3d.com/ScriptReference/Component-gameObject.html
public void _KillEnemy(Enemy _enemy)
{
GameObject _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity).gameObject;
Destroy(_clone, 5f);
cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
Destroy(_enemy.gameObject);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.