Photon Sync Var
So i'm working on this project and i had a lot of fun working on it on single player but i felt that it would be good to make it multiplayer i have no experience with multiplayer so i used "Photon" ,
first is this a close imagine with what i have
then we get this you join the server and you see the person who plays for a while like a some just loged in i want people to join anytime and leave anytime
https://www.dropbox.com/home?preview=3.PNG
-so i have the codes here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerItems : Photon.MonoBehaviour {
public float hs;
public float Health;
public int RockAmouts;
public int ActiveRocks;
public Text Rocksa;
[PunRPC]
public void SetMyRocks(int Number){
RockAmouts += Number;
Debug.Log("player: "+RockAmouts);
Rocksa.text = "" + RockAmouts;
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting) {
//we own it
stream.SendNext (RockAmouts);
} else {
ActiveRocks = (int)stream.ReceiveNext();
}
}
}
And i set the points here
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class ItemMine : Photon.MonoBehaviour { public PlayerItems Pt; public FindLocal fl; public bool destroya; public Sprite[] Sprites; public void OnTriggerEnter2D(Collider2D other) { if (fl.localPlayer) { PhotonView photonView = fl.localPlayer.GetComponent<PhotonView> (); if (other.tag == "Player") { fl.Itema = fl.localPlayer.GetComponent<PlayerItems> (); photonView.RPC ("SetMyRocks", PhotonTargets., 1); // Call to RPC function if (destroya == true) { PhotonNetwork.Destroy (gameObject); } } } } void OnMouseDown (){ Debug.Log("V IS BEING PRESSED!!"); // int projectileAttackRandomizer = Random.Range (1,Sprites.Length); GameObject rocks = PhotonNetwork.Instantiate("RockPoint", this.transform.position, this.transform.rotation, 0); // rocks.GetComponent<SpriteRenderer>().sprite = Sprites[projectileAttackRandomizer -= 1]; } }
and Finally finding my local player
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FindLocal : Photon.MonoBehaviour { public GameObject localPlayer; public Transform Caamera; public PlayerItems Itema; public void Update() { if (localPlayer == null) { foreach (GameObject cur in GameObject.FindGameObjectsWithTag("Player")) { Debug.Log (cur); Debug.Log ("Iterating"); if (cur.GetComponent<PhotonView> ().photonView.isMine == true) { Debug.Log ("Found local player"); localPlayer = cur; } } } if (localPlayer == null) { // Debug.Log ("Couldn't find local player"); } if (localPlayer != null) { // localPlayer.Itema if(Caamera == null){ Caamera = Camera.main.transform; } if (Itema == null) { Itema = localPlayer.GetComponent<PlayerItems> (); } } } }
Answer by Joey_pc · Sep 03, 2017 at 07:37 PM
I Fixed It By doing this #if UNITY_5 && (!UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3) || UNITY_6 #define UNITY_MIN_5_4 #endif using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerItems : Photon.MonoBehaviour { public float hs; public float Health; [Tooltip("The current Rocks of our player")] public int RockAmouts; public int ActiveRocks; public Text Rocksa; [PunRPC] void Update(){ this.Rocksa.text = "" + RockAmouts; } public void SetMyRocks(int Number){ RockAmouts += Number; Debug.Log("player: "+RockAmouts); } #region IPunObservable implementation public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.isWriting) { // We own this player: send the others our data stream.SendNext(this.RockAmouts); } else { // Network player, receive data this.RockAmouts = (int)stream.ReceiveNext(); } } #endregion }
and simply drag and drop the scirpt here
Thank you so much for this! Your example worked perfectly for what I was trying to accomplish! I've been searching for weeks now, your revised script worked for what I was trying to do! Thank You!
Your answer
Follow this Question
Related Questions
How to sync RPC function Photon Network? 1 Answer
Photon & Unity Question 0 Answers
[SOLVED] PhotonNetwork.Instantiate cannot spawn players 0 Answers
Unity - Photon OnJoinedRoom not being called 0 Answers
Sync movements Photon 0 Answers