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

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

RoyKent
2020-06-20 18:44:31

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;
}
}

Comments

DarkJoltGames
2020-06-21 23:55:52

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
2020-07-02 17:45:58

DarkJoltGames your a hero, thank u sooo much it worked!!

Back to top