列表元素显示在检查器中,但不在脚本引用中

时间:2019-06-24 17:08:01

标签: c# unity3d

我有一个list_of_targetable列表,可以在其中生成敌人实例。我可以在检查器的播放过程中看到实例正确生成,并正确放置在列表中。

当我尝试从同一脚本中引用这些实例时,它们似乎不存在,并且list.Count返回0。此外,TargetEnemySlot1之后很久,当敌人被初始化时,我打电话给Start的方式就是通过按钮。

有什么作用?

public class Targeting : MonoBehaviour
{
    public List<Combatant> current_targets;
    public List<Combatant> list_of_targetable;

    public void TargetEnemySlot1()
    {
        Debug.Log(list_of_targetable.Count);
        current_targets.Add(list_of_targetable[0]);
        current_targets[0].is_target = true;
        current_targets[0].targeting_panel.SetActive(false);
    }
}

与列表直接互动的事物:

public class AbAttack : MonoBehaviour, IAbility
{
public Targeting targeting_for_attack;

public int block_attack_damage = 10;
public int block_attack_repeats = 2;

public bool needs_targets = true;
public bool targets_allies = false;
public bool targets_enemies = true;
public int number_of_targets = 1;


public void Use()
{
    if (needs_targets == true)
    {
        if (targets_enemies == true)
        {
            foreach (Combatant enemy in targeting_for_attack.list_of_targetable)
            {
                enemy.targeting_panel.SetActive(true);

            }
        }
    }
}

public void Enact()
{
    foreach (Combatant enemy in targeting_for_attack.current_targets)
    {
        for (int i = 0; i < block_attack_repeats; i++)
        {
            enemy.health = enemy.health - block_attack_damage;
        }

    }
}
}

public class EnemySpawner : MonoBehaviour
{
public List<Combatant> CombatantList = new List<Combatant>();
public List<Combatant> CombatantInWaitingList = new List<Combatant>();
public List<Combatant> CombatantInCombatList = new List<Combatant>();

Inventory inventory;

public List <GameObject> targeting_panel;
public Targeting targeting_panel_targeting;
public Sprite icon1;

// Start is called before the first frame update
void Start()
{

    inventory = Inventory.instance;

    DrainList();
    SpawnList();

}

public void DrainList()
{
    List<Combatant> ToRemoveList = new List<Combatant>();

    foreach (Combatant item in CombatantInWaitingList)
    {
        if (CombatantInCombatList.Count <= 3)
        {
            CombatantInCombatList.Add(item);
            ToRemoveList.Add(item);
        }
    }

    foreach (Combatant item in ToRemoveList)
    {
        CombatantInWaitingList.Remove(item);
    }
}

public void SpawnList()
{
    List<Combatant> ToRemoveList = new List <Combatant>();

    foreach (Combatant item in CombatantInCombatList)
    {
        if (Inventory.instance.items1.Count == 0)
        {
            Combatant data1 = Combatant.CreateInstance("spider", icon1, targeting_panel[0]);
            Inventory.instance.items1.Add(data1);
            targeting_panel_targeting.list_of_targetable.Add(data1);
            ToRemoveList.Add(item);
        }

        if (Inventory.instance.items1.Count == 1)
        {
            Combatant data1 = Combatant.CreateInstance("spider", icon1, targeting_panel[1]);
            Inventory.instance.items1.Add(data1);
            targeting_panel_targeting.list_of_targetable.Add(data1);
            ToRemoveList.Add(item);
        }

        if (Inventory.instance.items1.Count == 2)
        {
            Combatant data1 = Combatant.CreateInstance("spider", icon1, targeting_panel[2]);
            Inventory.instance.items1.Add(data1);
            targeting_panel_targeting.list_of_targetable.Add(data1);
            ToRemoveList.Add(item);
        }

    }

    foreach (Combatant item in ToRemoveList)
    {
        // Add new combataant at that list location FIRST
        CombatantInCombatList.Remove(item);
    }
}
}

实例正确生成的IN GAME检查器: http://prntscr.com/o629xb

0 个答案:

没有答案