In my game i have a menu and there you can set a FireDamage variable. It is static and everything works fine with variables over scenes. But when i try to kick the other player out when it does not has the same variable value it does not work. Here my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PhotonManager : MonoBehaviour
{
public GameObject player;
private GameObject HostPlayer;
private GameObject JoinedPlayer;
[SerializeField] private GameObject LobbyCamera;
private void Start()
{
PhotonNetwork.ConnectUsingSettings("1.0");
}
void OnJoinedLobby()
{
PhotonNetwork.JoinOrCreateRoom("Room", new RoomOptions() { MaxPlayers = 2 }, TypedLobby.Default);
}
void OnJoinedRoom()
{
if (PhotonNetwork.isMasterClient)
{
HostPlayer = PhotonNetwork.Instantiate("Player", player.transform.position, Quaternion.identity, 0) as GameObject;
LobbyCamera.SetActive(false);
} else if (!PhotonNetwork.isMasterClient)
{
JoinedPlayer = PhotonNetwork.Instantiate("Player", player.transform.position, Quaternion.identity, 0) as GameObject;
LobbyCamera.SetActive(false);
if (JoinedPlayer.GetComponent<OnOff>().FireDamage != HostPlayer.GetComponent<OnOff>().FireDamage)
{
PhotonNetwork.Disconnect();
SceneManager.LoadScene(0);
}
}
}
}
Hey there, i can see where you try to go but the way you approach this issue is a bit wrong.
The point here is the following: Assuming we both have the same program, but i modify mine to have another dmg value on the player prefab. If i join my game i call `PhotonNetwork.Instantiate(“Player”,…)
This will create an instantiation request, but this will not transfer the gameobject/prefab. It only tells your instance to search for the “Player” prefab and instantiate it. So in the end we both instantiate different prefabs with different values and you will never know.
So in the end you have to manually send these values after instantiation for validation by using some rpc for example.
At this point please not that even that is not completly safe if you assume that someone knows what he’s doing. The only really completly safe option is doing damage calculation on a dedicated server instance.
ive tried a bit around with rpc but it still seems not to work
my script which i posted a day ago.
And here is my second script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class OnOff : Photon.PunBehaviour
{
public GameObject Button;
static bool StaticFireDamage = true;
public bool FireDamage = true;
public bool isUI;
public virtual void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
// only add variables here that basically change every frame, like positions and rotations ( mostly already handled by networktransform)
if (stream.isWriting)
{
}
else if (stream.isReading)
{
}
}
public void ChangeButton(bool button)
{
Button.SetActive(true);
StaticFireDamage = button;
gameObject.SetActive(false);
}
public void LoadScene()
{
SceneManager.LoadScene(1);
}
void Update()
{
FireDamage = StaticFireDamage;
if (isUI == false)
{
photonView.RPC("SyncFireDamage", PhotonTargets.MasterClient, StaticFireDamage);
}
}
[PunRPC]
void SyncFireDamage(bool statisch)
{
FireDamage = statisch;
}
}
This is the first scene where i have to choose my options:
And this is my Ingame scene: