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.
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.
Cast it to a Gun
and it should stop complaining:
equippedGun = (Gun)Instantiate(gunToEquip, WeaponHold.position, WeaponHold.rotation);
Make sure your class inherits from MonoBehaviour.
public class MyClass : MonoBehaviour{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.