Please help, trying to sync flashlight over network,Please Help, i need to sync flashlight over netw

Options
Hello everyone, as it seems, im new to all this im making fps multiplayer game in the game i have 2 players that have flashlights, when one of them light his the second doesn't see it i tried with this script but it seems something is missing

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

public class FlashlightTP : MonoBehaviourPunCallbacks {

public bool isOn = false;
public GameObject lightSrource;
public AudioSource clickSound;
public bool failSafe = false;
// Update is called once per frame
void Update()
{
if (!photonView.IsMine)
return;
if (Input.GetKeyDown(KeyCode.L))
{
if (isOn == false && failSafe == false)
{
failSafe = true;
lightSrource.SetActive(true);
clickSound.Play();
isOn = true;
StartCoroutine(FailSafe());
}
if (isOn == true && failSafe == false)
{
failSafe = true;
lightSrource.SetActive(false);
clickSound.Play();
isOn = false;
StartCoroutine(FailSafe());
}
}
IEnumerator FailSafe()
{
yield return new WaitForSeconds(0.25f);
failSafe = false;
}
}

Best Answers

  • DarkJoltGames
    DarkJoltGames ✭✭
    Answer ✓
    Options
    What you're looking for is an RPC.

    Do this instead:

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

    public class FlashlightTP : MonoBehaviourPunCallbacks {

    public bool isOn = false;
    public GameObject lightSrource;
    public AudioSource clickSound;
    public bool failSafe = false;
    public PhotonView PV;
    // Update is called once per frame
    void Update()
    {
    if (!photonView.IsMine)
    return;
    if (Input.GetKeyDown(KeyCode.L))
    {
    if (isOn == false && failSafe == false)
    {
    PV.RPC("TurnOn",PhotonTargets.All);
    }
    if (isOn == true && failSafe == false)
    {
    PV.RPC("TurnOff",PhotonTargets.All);
    }
    }
    IEnumerator FailSafe()
    {
    yield return new WaitForSeconds(0.25f);
    failSafe = false;
    }
    [PunRPC]
    void On(){
    failSafe = true;
    lightSrource.SetActive(true);
    clickSound.Play();
    isOn = true;
    StartCoroutine(FailSafe());
    }
    [PunRPC]
    void Off(){
    failSafe = true;
    lightSrource.SetActive(false);
    clickSound.Play();
    isOn = false;
    StartCoroutine(FailSafe());
    }
    }

    RPCs are like magic!
  • RoyKent
    RoyKent
    Answer ✓
    Options
    DarkJoltGames your a hero, thank u sooo much it worked!!

Answers

  • DarkJoltGames
    DarkJoltGames ✭✭
    Answer ✓
    Options
    What you're looking for is an RPC.

    Do this instead:

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

    public class FlashlightTP : MonoBehaviourPunCallbacks {

    public bool isOn = false;
    public GameObject lightSrource;
    public AudioSource clickSound;
    public bool failSafe = false;
    public PhotonView PV;
    // Update is called once per frame
    void Update()
    {
    if (!photonView.IsMine)
    return;
    if (Input.GetKeyDown(KeyCode.L))
    {
    if (isOn == false && failSafe == false)
    {
    PV.RPC("TurnOn",PhotonTargets.All);
    }
    if (isOn == true && failSafe == false)
    {
    PV.RPC("TurnOff",PhotonTargets.All);
    }
    }
    IEnumerator FailSafe()
    {
    yield return new WaitForSeconds(0.25f);
    failSafe = false;
    }
    [PunRPC]
    void On(){
    failSafe = true;
    lightSrource.SetActive(true);
    clickSound.Play();
    isOn = true;
    StartCoroutine(FailSafe());
    }
    [PunRPC]
    void Off(){
    failSafe = true;
    lightSrource.SetActive(false);
    clickSound.Play();
    isOn = false;
    StartCoroutine(FailSafe());
    }
    }

    RPCs are like magic!
  • RoyKent
    RoyKent
    Answer ✓
    Options
    DarkJoltGames your a hero, thank u sooo much it worked!!