【发布时间】:2018-11-16 20:51:44
【问题描述】:
我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BattleCore : MonoBehaviour {
public List<GameObject> Enemy = new List<GameObject> ();
public List<EnemyFollow1> EnemyScript = new List<EnemyFollow1>();
public List<StatData> stat = new List<StatData> ();
public int length;
public bool canMelee = false;
public List<ParticleSystem> ps = new List<ParticleSystem> ();
public float AttackCooldownT = 0;
public float AttackCooldownTI = 3;
void OnTriggerEnter(Collider other)
{
Debug.Log("ENTERED!");
if (other.CompareTag("Enemy"))
{
canMelee = true;
Enemy.Add (other.gameObject);
for (int i = 0; i < Enemy.Count; i++)
{
ps.Add(Enemy[i].GetComponent<ParticleSystem>());
EnemyScript.Add(Enemy[i].GetComponent<EnemyFollow1>());
stat.Add(EnemyScript[i].enemyData);
}
}
}
void OnTriggerExit(Collider other) {
if (other.CompareTag("Enemy"))
{
canMelee = false;
for (int i = 0; i < length; i++)
{
if (Enemy[i] == other.gameObject)
{
Enemy.RemoveAt(i);
stat.RemoveAt(i);
EnemyScript.RemoveAt(i);
ps.RemoveAt (i);
}
}
}
}
void Update()
{
if (canMelee && Input.GetKeyDown(KeyCode.Q) && Time.time > AttackCooldownT && Enemy.Count > 0)
{
for (int i = 0; i < Enemy.Count; i++)
{
stat[i].HP -= 20;
ps [i].Play ();
}
AttackCooldownT = Time.time + AttackCooldownTI;
}
}
}
我创建了一些对象(敌人)的列表,其中包含一些我用统一的可脚本对象制作的数据列表。这个脚本添加和删除敌人和 这个脚本工作正常,但是当有 2 个敌人时,许多敌人(同一个游戏对象开始多次添加)并且在删除所有数据时不会被删除,所以即使我杀死了一个敌人,他仍然在场景中。
我的项目数据:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class StatData : ScriptableObject {
public string Name;
public bool Boss;
public int HP, MaxHP, Atk, MaxAtk, SpAtk, MaxSpAtk, Def, MaxDef, SpDef, MaxSpDef;
public Sprite sprite;
}
另外,当我试图用相同的统计数据杀死一个敌人时,两个敌人同时死亡,而我只攻击一个。由于健康是全球性的,两者都会死亡。我不知道如何解决它。
在我的敌人跟随我写道:
enemyStat.HP = enemyStat.MaxHP;
在新游戏中让敌人活着。 请帮忙!
【问题讨论】:
-
短语“当我试图杀死一个具有相同统计数据的敌人时,两个敌人一起死,而我只攻击一个”表示您可能复制了对对象的引用,而不是创建一个新的副本那个物体。因此,您的列表包含对同一对象的两个引用。
-
我想帮助你,但是我注意到你还有一些其他的问题显然有答案,但你没有接受它们作为答案。
-
Richardissimo 我趋之若鹜,但没有奏效。我应该做公众浮动健康吗?