Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheGeorg · May 22, 2019 at 07:43 PM · networkingmultiplayerphotonsynchronize

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheGeorg · May 23, 2019 at 03:50 PM 0
Share

thanks for your answer

avatar image TheGeorg · May 24, 2019 at 02:57 PM 0
Share

please help me

avatar image
0

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: alt text

And this is my Ingame scene: alt text


ingamescreenshot.png (46.5 kB)
startscreem.png (6.8 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · May 25, 2019 at 10:07 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges