Player reviving not working

Options

Hello!

I am making my multiplayer game and trying to fix my broken player reviving script. The problem is that the playercontroller's isDead bool and the animator's isDead bool do not update on all clients for some reason. This is the ReviveScript.

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

public class ReviveScript : MonoBehaviourPun
{
    public HPText hpText;

    [Header("Reviving")]
    public float reviveTime;
    public bool isTouchingDeadPerson;
    public PlayerController playerToRevive;
    public PlayerController playerToReviveForAllClients;
    public GameOverScript gameOverScript;

    private void Start()
    {
        hpText = GetComponent<HPText>();
        gameOverScript = FindObjectOfType<GameOverScript>();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (photonView.IsMine)
        {
            if (collision.CompareTag("Player"))
            {
                if (collision.GetComponent<PlayerController>().isDead)
                {
                    isTouchingDeadPerson = true;
                    playerToRevive = collision.GetComponent<PlayerController>();
                }
            }
        }
    }

    private void Update()
    {
        if(photonView.IsMine && isTouchingDeadPerson && Input.GetKeyDown(KeyCode.Z))
        {
            photonView.RPC("RevivePlayerTimer", RpcTarget.All);
            Debug.LogError("reviving...");
            photonView.RPC("SetPlayerToReviveForAllClients", RpcTarget.All, playerToRevive);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (photonView.IsMine)
        {
            if (collision.CompareTag("Player"))
            {
                if (collision.GetComponent<PlayerController>().isDead)
                {
                    isTouchingDeadPerson = false;
                    playerToRevive = null;
                }
            }
        }
    }

    [PunRPC]
    IEnumerator RevivePlayerTimer()
    {
        yield return new WaitForSeconds(reviveTime);

        playerToReviveForAllClients.GetComponent<Animator>().SetBool("isDead", false);

        playerToReviveForAllClients.currentHP = 1;
        playerToReviveForAllClients.isDead = false;

        if (photonView.IsMine)
        {
            gameOverScript.FindPlayers();

            //photonView.RPC("RevivePlayer", RpcTarget.All);
            Debug.Log(playerToRevive + "Revived!");
            playerToRevive = null;
        }
    }

    [PunRPC]
    void SetPlayerToReviveForAllClients(PlayerController _playerToRevive)
    {
        playerToReviveForAllClients = _playerToRevive;
    }
}

If someone knows why this isn't working or how to fix it I'd appreciate any help. thanks!

Answers

  • Klover
    Klover ✭✭
    edited December 2021
    Options

    If you want to sync a variable it has to be a parameter in the RPC function.


    bool isDead;


    [PunRPC]

    void SyncMyBool (bool variableToSync)

    {

    isDead = variableToSync;

    }


    public void CallFunction()

    {

    photonView.RPC("SyncMyBool", RpcTarget.All, isDead); //You see how we added in your isDead bool here?

    //When this function is called for all clients, the bool will be synced because you added in YOUR OWN isDead bool as the parameter, all clients will get what you're sending out.

    }