简体   繁体   English

无法将类型“UnityEngine.Transform”转换为“UnityEngine.GameObject”

[英]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 .您不能将TransformGameObject 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.

相关问题 无法将类型“UnityEngine.GameObject”隐式转换为“GameObject” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameObject' 无法将类型&#39;Vuforia.Anchor&#39;隐式转换为&#39;UnityEngine.GameObject&#39; - Cannot implicitly convert type `Vuforia.Anchor' to 'UnityEngine.GameObject' 如何将类型“int”转换为“unityengine.gameobject”? - How to convert type 'int' to 'unityengine.gameobject'? 无法将类型“UnityEngine.GameObject”隐式转换为“GameMaster”? - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameMaster'? 尝试实例化 object 时无法将类型“UnityEngine.GameObject”隐式转换为“UnityEngine.Vector2” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Vector2'" While trying to Instantiate an object 无法将类型“UnityEngine.Transform”隐式转换为“UnityEngine.UI.Text” - Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.UI.Text' 无法从“UnityEngine.GameObject”转换为“System.Predicate”<unityengine.gameobject> '</unityengine.gameobject> - Cannot convert from 'UnityEngine.GameObject' to 'System.Predicate<UnityEngine.GameObject>' foreach语句无法遍历UnityEngine.GameObject类型的变量 - foreach statement cannot ooperate on variables of type UnityEngine.GameObject 无法转换 Type 'UnityEngine.Rigidbody' en 'UnityEngine.GameObject 并且没有 Addforce 的定义 - Can't convert Type 'UnityEngine.Rigidbody' en 'UnityEngine.GameObject and no definition for Addforce SerializationException:类型UnityEngine.GameObject未标记为Serializable - SerializationException: Type UnityEngine.GameObject is not marked as Serializable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM