- Home /
photon variable script does not work :(
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);
}
}
}
}
Answer by Captain_Pineapple · May 23, 2019 at 09:03 AM
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.
Answer by TheGeorg · May 23, 2019 at 04:34 PM
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:
Hey there,
please - for your own good - do not add code where a rpc might be called every update! This might result in ridiculous amounts of messages per second. To test stuff like that add something like "send the rpc when F5 is pressed"
also your issue is that even if player 2 gets the rpc (where you should add a Debug.Log to check if the message arrives...) the value is instantly overwritten since you have FireDamage = StaticFireDamage;
in your update.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201908im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
hovering usernames with photon 1 Answer
Best Solution for Multiplayer Shooting 1 Answer
How to implement server side logic with PHOTON? 0 Answers
Access host's object 0 Answers