Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用FindGameObjecstWithTag获取组件>;_C#_Unity3d_Multiplayer - Fatal编程技术网

C# 如何使用FindGameObjecstWithTag获取组件>;

C# 如何使用FindGameObjecstWithTag获取组件>;,c#,unity3d,multiplayer,C#,Unity3d,Multiplayer,我正在使用unity构建一个多人游戏。主播放器是使用网络管理器生成的预置,包含player标记和。下面是敌人的攻击脚本,它搜索玩家标签,但在多人游戏中只能得到一个玩家。我如何更改它,使游戏对象玩家成为一个阵列,玩家健康可以获得组件玩家健康 public class EnemyAttack : MonoBehaviour { public float timeBetweenAttacks = 0.5f; // The time in seconds between each att

我正在使用unity构建一个多人游戏。主播放器是使用网络管理器生成的预置,包含player标记和。下面是敌人的攻击脚本,它搜索玩家标签,但在多人游戏中只能得到一个玩家。我如何更改它,使游戏对象玩家成为一个阵列,玩家健康可以获得组件玩家健康

public class EnemyAttack : MonoBehaviour
{
    public float timeBetweenAttacks = 0.5f;     // The time in seconds between each attack.
    public int attackDamage = 10;               // The amount of health taken away per attack.


    Animator anim;                              // Reference to the animator component.
    GameObject player;                          // Reference to the player GameObject.
    PlayerHealth playerHealth;                  // Reference to the player's health.
    EnemyHealth enemyHealth;                    // Reference to this enemy's health.
    bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
    float timer;                                // Timer for counting up to the next attack.


    void Awake ()
    {
        // Setting up the references.
        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();
        enemyHealth = GetComponent<EnemyHealth>();
        anim = GetComponent <Animator> ();
    }


    void OnTriggerEnter (Collider other)
    {
        // If the entering collider is the player...
        if(other.gameObject == player)
        {
            // ... the player is in range.
            playerInRange = true;
        }
    }


    void OnTriggerExit (Collider other)
    {
        // If the exiting collider is the player...
        if(other.gameObject == player)
        {
            // ... the player is no longer in range.
            playerInRange = false;
        }
    }


    void Update ()
    {
        // Add the time since Update was last called to the timer.
        timer += Time.deltaTime;

        // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
        if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
        {
            // ... attack.
            Attack ();
        }

        // If the player has zero or less health...
        if(playerHealth.currentHealth <= 0)
        {
            // ... tell the animator the player is dead.
            anim.SetTrigger ("PlayerDead");
        }
    }


    void Attack ()
    {
        // Reset the timer.
        timer = 0f;

        // If the player has health to lose...
        if(playerHealth.currentHealth > 0)
        {
            // ... damage the player.
            playerHealth.TakeDamage (attackDamage);
        }
    }
}
公共类EnemyAttack:MonoBehavior
{
public float timeBetweenAttacks=0.5f;//每次攻击之间的时间(以秒为单位)。
public int attackDamage=10;//每次攻击夺走的生命值。
Animator anim;//对Animator组件的引用。
GameObject player;//对玩家GameObject的引用。
PlayerHealth PlayerHealth;//对玩家健康的引用。
EnemyHealth EnemyHealth;//引用此敌人的健康。
bool PlayerRange;//玩家是否在触发碰撞器内并且可以被攻击。
float timer;//计数到下一次攻击的计时器。
无效唤醒()
{
//设置引用。
player=GameObject.FindGameObjectWithTag(“player”);
playerHealth=player.GetComponent();
enemyHealth=GetComponent();
anim=GetComponent();
}
无效对撞机(对撞机其他)
{
//如果进入的碰撞器是玩家。。。
if(other.gameObject==玩家)
{
//…玩家在射程内。
playerlange=true;
}
}
void OnTriggerExit(碰撞器其他)
{
//如果退出的碰撞器是玩家。。。
if(other.gameObject==玩家)
{
//…玩家不再在射程内。
playerRange=false;
}
}
无效更新()
{
//将上次调用更新后的时间添加到计时器。
timer+=Time.deltaTime;
//如果计时器超过两次攻击之间的时间,则玩家在射程内且该敌人还活着。。。
if(timer>=timebetweethacks&&playerRange&&enemyHealth.currentHealth>0)
{
//…攻击。
攻击();
}
//如果玩家的生命值为零或更少。。。
if(playerHealth.currentHealth 0)
{
//…伤害玩家。
玩家健康。承受伤害(攻击伤害);
}
}
}
您应该需要与一起使用。下面给出了一个未经测试的代码段:

公共游戏对象[]玩家列表

void Start() {
          //Invokes the method methodName in time seconds,
          //then repeatedly every repeatRate seconds.
          InvokeRepeating("CheckPlayers", 2.0f, 0.3f);
    }


public void CheckPlayers(){
       //assign player in the player list
       playersList= GameObject.FindGameObjectsWithTag ("Player");
      //now playersList contains all player, do what you want with it

}

您是否正在尝试存储多个玩家对象和多个玩家健康参考?您是否尝试过使用
FindGameObjectsWithTag()
并…迭代结果?我尝试过,但没有帮助,因为出现了错误“System.Array”不包含“GetComponent”的定义您计划如何在当前脚本中使用多个播放器/健康?如果您将所有这些文件都存储在一个集合中,您将如何确定哪个文件受到
攻击()
的影响?敌人能同时攻击范围内的所有玩家吗?还是一定要选一个?我觉得在它目前的状态下,这个问题的范围不仅仅是正确地检索和存储一组玩家-你也需要重新修改你的游戏逻辑。是的,敌人同时攻击所有玩家。我有一个不同的脚本来产生敌人和一个不同的敌人,你可能想考虑玩家处理他们是否已经死亡的逻辑-在责任方面,这似乎是敌人不必担心的事情。