简体   繁体   English

CS0266 C# 无法将类型“UnityEngine.Object”隐式转换为“”。 存在显式转换(您是否缺少演员表?)

[英]CS0266 C# Cannot implicitly convert type 'UnityEngine.Object' to ''. An explicit conversion exists (are you missing a cast?)

I have a compiler error where the compiler cannot convert type "UnityEngine.Object to Gun. I don't think that these are different file types, but anyway, here's my code. I've been stumped on this and I'm no pro on C# yet, so forgive me if it's a n00b mistake. Idk how to use the code insert on this, it confuses me, so here's a printscreen.我有一个编译器错误,编译器无法将类型“UnityEngine.Object 转换为 Gun。我不认为这些是不同的文件类型,但无论如何,这是我的代码。我一直被这个问题难住,我不是专业人士在 C# 上,所以如果是 n00b 错误,请原谅我。我不知道如何使用代码插入,这让我很困惑,所以这是一个打印屏幕。

using UnityEngine;
using System.Collections;

public class GunController : MonoBehaviour {

    public Transform WeaponHold;
    public Gun startingGun;
    Gun equippedGun;

    void Start()
    {
        if (startingGun != null)
        {
            EquipGun(startingGun);
        }
    }

    public void EquipGun(Gun gunToEquip)
    {
        if(equippedGun != null)
        {
            Destroy(equippedGun.gameObject);
        }
        equippedGun = Instantiate(gunToEquip, WeaponHold.position,WeaponHold.rotation);
        equippedGun.transform.parent = WeaponHold;
    }
}

TheInstantiate method returns an object , which you can cast to the necessary type. Instantiate方法返回一个object ,您可以将其转换为所需的类型。

Cast it to a Gun and it should stop complaining:把它Gun ,它应该停止抱怨:

equippedGun = (Gun)Instantiate(gunToEquip, WeaponHold.position, WeaponHold.rotation);

Make sure your class inherits from MonoBehaviour.确保您的类继承自 MonoBehaviour。

public class MyClass : MonoBehaviour{

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:CS0266无法将类型'int'隐式转换为'byte'。 存在显式转换(您是否缺少演员表?) - Error: CS0266 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) 错误 CS0266:无法将类型“float”隐式转换为“int”。 存在显式转换(您是否缺少演员表?) - error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) 错误 CS0266 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“字符串”。 存在显式转换(您是否缺少演员表?) - Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?) 错误CS0266:无法将类型'object'隐式转换为'int' - error CS0266: Cannot implicitly convert type 'object' to 'int' 无法隐式转换类型,存在显式转换(您是否缺少强制转换?) - Cannot implicitly convert type, an explicit conversion exists (are you missing a cast?) C#错误CS0266:无法将类型“狗”隐式转换为“ Rottweiler” - C# error CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler 无法隐式转换类型,存在显式转换(您是否缺少强制转换?) - Cannot implicitly convert type, explicit conversion exists (are you missing a cast?) 无法将类型“object”隐式转换为“object[*,*]”。 存在显式转换(您是否缺少演员表) - Cannot implicitly convert type 'object' to 'object[*,*]'. An explicit conversion exists (are you missing a cast) 错误26无法将类型'object'隐式转换为'string'。 存在显式转换(您是否缺少演员表?) - Error 26 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) 无法将类型'object'隐式转换为'string'。 存在显式转换(您是否缺少演员表?) - Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM