Problem with my Enemy Versus Script..

Hello everyone!

I’ve decided to make a multiplayer horror game which is like Dead by Daylight, where the host is the monster and he need to kill all the player which are alive. But Dead by Daylight has random chooser, well I don’t really need random killer chooser in my game.

So… if there are 2 survivor players in the game, and when I want kill the 1st player, then the game destroys the 2nd player.

So that’s the problem for me… Could someone help to fix this one little problem?

Here’s the Enemy Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.SceneManagement;

public class EnemyScript_Versus : MonoBehaviourPunCallbacks
{

public PhotonView PV;

public GameObject[] players;

public int currentHealth;

public int maxHealth = 100;

private int minHealth = 0;

private int damageAmount = 50;

[SerializeField]
private Animator Anim;

public bool onTrig;

[SerializeField]
private float distanceToSurvivor = 1f;

void Start () {
    currentHealth = maxHealth;
}

void Update() {

    if (PauseMenu_Multiplayer.onPauseMenuOpen)
    {
        return;
    }

    players = GameObject.FindGameObjectsWithTag("Player");

    if (currentHealth > maxHealth)
    {
        currentHealth = maxHealth;
    }
    if(currentHealth < minHealth)
    {
        currentHealth = minHealth;
    }

    foreach (GameObject playerGO in players)
    {
        float distance = Vector3.Distance(transform.position, playerGO.transform.position);

        if (photonView.IsMine)
        {
            if (distance < distanceToSurvivor)
            {
                RaycastHit hit;
                Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
                if (Physics.Raycast(ray, out hit, distanceToSurvivor))
                {
                    foreach (GameObject go in players)
                    {
                        if (hit.transform.position == go.transform.localPosition)
                        {
                            Debug.Log(go.transform.localPosition - transform.localPosition);
                            if (Input.GetMouseButtonDown(0))
                            {
                                Attacking();
                            }
                        }
                    }
                }
            }
        }
    }
}

public void Damaging()
{
    PlayerHealth[] PH = GameObject.FindWithTag("Player").GetComponents<PlayerHealth>();
    PhotonView[] PV = GameObject.FindWithTag("Player").GetComponents<PhotonView>();

    foreach(PlayerHealth pH in PH)
    {
        if(pH != null)
        {
            foreach (PhotonView PVm in PV)
            {
                if (photonView == null)
                {
                    Debug.LogError("PhotonView hasn't been attached.. Please make sure to attach your Photon View to this script.");
                }
                else
                {
                    PVm.RPC("Death", RpcTarget.AllBuffered);
                }
            }
        }

    }
}

public void Death()
{
    foreach(GameObject player in players)
    {
        PhotonNetwork.Destroy(player);
        Debug.Log(player.gameObject + " has been killed by the werewolf!");
    }

}

public void Attacking()
{
    Anim.SetBool("isIdle", false);
    Anim.SetBool("isWalking", false);
    Anim.SetBool("isRunning", false);
    Anim.SetBool("isAttacking", true);
    StartCoroutine(Timer3());
    StartCoroutine(Timer1());
}

IEnumerator Timer3()
{
    yield return new WaitForSeconds(1.15f);
    Anim.SetBool("isAttacking", false);
    Anim.SetBool("isIdle", true);
    Anim.SetBool("isWalking", false);
    Anim.SetBool("isRunning", false);

}

IEnumerator Timer1()
{
    yield return new WaitForSeconds(0.9f);
    Damaging();
}

}

Also there’s the Survivor Health Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using System;

public class PlayerHealth : MonoBehaviourPunCallbacks {

public int maxHealth = 100;

private EnemyScript_Versus _enemyScript;

public int currentHealth;

void Start () {
    currentHealth = maxHealth;
}

/**[PunRPC]
public void TakeDamage(int amt)
{
    if (photonView.IsMine)
    {
        currentHealth -= amt;

        if (currentHealth <= 0)
        {
            Death();
        }
    }
}*/

[PunRPC]
void Death()
{
    if (photonView.IsMine == true && PhotonNetwork.IsConnected == true)
    {
        PhotonNetwork.Destroy(this.gameObject);
        Debug.Log(gameObject + " has killed by the werewolf!");
        _enemyScript = GameObject.Find("Enemy_Multiplayer(Clone)").GetComponent<EnemyScript_Versus>();
    }
}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
    if (stream.IsWriting)
    {
        stream.SendNext(currentHealth);
    }
    if(stream.IsReading)
    {
        stream.ReceiveNext();
    }
}

}

I’m still practicing myself on creating multiplayer scripts. Anyways, thank you in advance! ^^

So its not easy to understand everything you are trying to do in your game, but what i did notice is that you use this piece of code:

public void Death()
 {
     foreach(GameObject player in players)
     {
         PhotonNetwork.Destroy(player);
         Debug.Log(player.gameObject + " has been killed by the werewolf!");
     }
}

This would mean that you use an array or a list of some kind to store your players. When the death function is called, it will loop through all the players and Destroy() them. You would need to add an extra check to see if the player is actually dead, by using a bool in the player script or something. Something like this:

public void Death()
 {
     foreach(GameObject player in players)
     {
         if(!player.dead){continue;}

         PhotonNetwork.Destroy(player);
         Debug.Log(player.gameObject + " has been killed by the werewolf!");
     }
}

Hope this helps you. If not, please provide some extra information about the problem. Also try to single out the source of the problem instead of posting all the scripts. This will make it easier for people to help you with your problem.