无法访问数组中抽象 class 实例的字段

Can't access fields of the abstract class instances in an array

我正在尝试统一构建一个 FPS 游戏,并且正在尝试通过角色实现武器装备。 为此,我有:

武器class:

public abstract class Weapon : MonoBehaviour { 
    lots of weapon code
}

机枪class:

public class MachineGun : Weapon{
    lots of machinegun code
}

在角色 class 中,我这样做:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//keeps track of all the character properties
public class Character : MonoBehaviour {

     public GameObject[] _equipedWeapons = new GameObject[10];     //an array of all the weapons a character has

private void Start()
{
    _equipedWeapons = new GameObject[10];                               //size of the weapons array, TEMP 10
    _equipedWeapons[0] = new GameObject();
    _equipedWeapons[0] = GameObject.FindWithTag("Weapon0");
}

这就是我的问题所在:

public void IncreaseBullets(int amount)
{
    _equipedWeapons[0].;
}

我似乎无法访问这把机枪的子弹数量。 事实上,我无法访问它自己的机枪字段,或者继承的武器字段。

我假设您已将 MachineGun 脚本附加到游戏对象。
您正在尝试访问 GameObject 属性。你要做的就是先拿到MachineGun Script

MachineGun weaponScript = _equipedWeapons[0].GetComponent<MachineGun>();
weaponScript.

你也可以这样获取武器脚本。当你想在同一个方法中访问不同的武器类型时。

Weapon weaponScript = _equipedWeapons[0].GetComponent<Weapon>();
weaponScript.