[英]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.