Search Unity

Third Party [Photon] Instantiating an enemy killable by all players

Discussion in 'Multiplayer' started by RavenUnchained, Mar 29, 2021.

  1. RavenUnchained

    RavenUnchained

    Joined:
    Sep 10, 2017
    Posts:
    1
    Hi everyone ! I'm slightly new to Photon Network and even by reading for a while the documentation, i couldn't find any solution to the problem i ran into.. I'm making a 2D coop platformer, and i'm trying to make an enemy spawn for all players when one of them runs close to a specific GameObject. The problem is, when using PhotonNetwork.Instantiate(...), my enemies spawns by 2/3/4 depending on the number of players, when i'm just expecting one, and each player can kill only one enemy among them all, the others will be unkillable for him. Ofc when using photonView.isMine, only one enemy does spawn, but i'm the only one who can kill it. I'm kinda runnin out ouf ideas, here is the code i'm using. And i would like to precise that my enemy has a photonView attached to it, along with a Photon Transform View and a Photon Animator View.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SlimeSpawn : MonoBehaviour
    6. {
    7.     private bool spawned=false;
    8.     public GameObject slimePrefab;
    9.     public PhotonView photonView;
    10.  
    11.     void OnTriggerEnter2D(Collider2D coll){
    12.  
    13.  
    14.       if(coll.gameObject.CompareTag("Player")&&spawned ==false){
    15.           spawned = true;
    16.           StartCoroutine(Spawning());
    17.       }
    18.  
    19.     }
    20.  
    21.     public IEnumerator Spawning(){
    22.       yield return new WaitForSeconds(0.5f);
    23.       PhotonNetwork.Instantiate("slimePrefab", this.transform.position, Quaternion.identity,0);
    24.       PhotonNetwork.Destroy(this.gameObject);
    25.     }
    26. }
    27.  
    If any of you has any idea, i'd be really grateful ! Thanks for reading !