The question is answered, right answer was accepted
Switch Script to enabled on each client(Photon)?
I'm a painfully new programmer(C# and Photon Networking) and i'm having trouble with a game i'm making. Its a kind of vampire survival game, like a survive the zombies game. I'm in the very early development stages. I have two different scripts depending on if your a vampire or human. When I press M(nothing fancy yet) you switch between the two. Each client is working, however is not changing on the other clients instance.
Example: Both players are human, one presses M becoming a vampire. On his screen he is a vampire. On the other persons screen he is still a human.
Thanks in advance, I will post scripts if needed.
UPDATE:While doing more googling I found photonView.ismine. It fixed my problem with responding to the M press however it doesn't fix enabling the scrips. I found hints to RPC however I didn't find how to do it. Does anyone know where to find an idiot's guide to it.
$$anonymous$$ Press Detector Script(HVController) All of these scripts are in an empty gameobject called SomeScripts
using UnityEngine; using System.Collections;
public class HVController : Photon.$$anonymous$$onoBehaviour {
public bool isVamp = false;
public bool isHum = true;
public GameObject eyes;
void Update(){
check$$anonymous$$orph ();
}
public void check$$anonymous$$orph(){
if(photonView.is$$anonymous$$ine){
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.$$anonymous$$) && isHum == true) {
isHum = false;
isVamp = true;
gameObject.GetComponent<HumanGenral> ().enabled = false;
gameObject.GetComponent<VampireGenral> ().enabled = true;
eyes.transform.tag = "VEyes";
}else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$) && isVamp == true){
isHum = true;
isVamp = false;
gameObject.GetComponent<HumanGenral> ().enabled = true;
gameObject.GetComponent<VampireGenral> ().enabled = false;
eyes.transform.tag = "Eyes";
}
}
}
public void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info){
if(stream.isWriting){
stream.SendNext (isVamp);
stream.SendNext(isHum);
}else{
isVamp = (bool)stream.ReceiveNext ();
isHum = (bool)stream.ReceiveNext ();
}
}
}
Vampire Script (VampireGenral)
using UnityEngine; using System.Collections;
public class VampireGenral : Human { RaycastHit hit$$anonymous$$emorize; public float memorizeReach = 20f; public Transform vEyes; public bool bool$$anonymous$$emorize = false;
void Update(){
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.$$anonymous$$ouse1) && !bool$$anonymous$$emorize) {
bool$$anonymous$$emorize = true;
}else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1) && bool$$anonymous$$emorize){
bool$$anonymous$$emorize = false;
}
$$anonymous$$emorize ();
Debug.DrawRay (raySpawn.transform.position, raySpawn.transform.forward * memorizeReach, Color.blue);
}
void $$anonymous$$emorize(){
if(bool$$anonymous$$emorize){
if(Physics.Raycast(raySpawn.transform.position, raySpawn.transform.forward, out hit$$anonymous$$emorize, memorizeReach)){
if(hit$$anonymous$$emorize.transform.tag == "Eyes"){
Debug.Log ("Found Eyes");
hit$$anonymous$$emorize.transform.Send$$anonymous$$essage("Start$$anonymous$$emorize", vEyes.transform);
Debug.Log("Sent");
}
}
}
}
public void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info){
if(stream.isWriting){
stream.SendNext (bool$$anonymous$$emorize);
}else{
bool$$anonymous$$emorize = (bool)stream.ReceiveNext ();
}
}
}
Human Script(HumanGenral)
using UnityEngine; using System.Collections;
public class HumanGenral : Human { RaycastHit eyesHit; public float eyesReach = 20f; public Camera myEyes; public bool memorized = false;
void Update () {
memorized = false;
Debug.DrawRay (raySpawn.transform.position, raySpawn.transform.forward * eyesReach, Color.green);
}
public void $$anonymous$$emorized(Transform vEyes){
memorized = true;
mainBody.GetComponent<Player$$anonymous$$ove> ().enabled = false;
cameraPlayer.GetComponent<$$anonymous$$y$$anonymous$$ouseLook> ().enabled = false;
cameraPlayer.GetComponent<Transform> ().LookAt (vEyes.transform, Vector3.up);
}
public void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info){
if(stream.isWriting){
stream.SendNext (memorized);
}else{
memorized = (bool)stream.ReceiveNext ();
}
}
}
The only ability the vampire has is memorize. It make the human look at them and disable the movement and look script
Answer by LoneWolf31322 · Aug 15, 2016 at 09:22 PM
After about an hour and a half I figured it out. I had to send and receive the enabled bool of the component.
using UnityEngine; using System.Collections; public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){ if(stream.isWriting){ stream.SendNext (isVamp); stream.SendNext(isHum); stream.SendNext (gameObject.GetComponent<HumanGenral> ().enabled); stream.SendNext (gameObject.GetComponent<VampireGenral> ().enabled); }else{ isVamp = (bool)stream.ReceiveNext (); isHum = (bool)stream.ReceiveNext (); gameObject.GetComponent<HumanGenral> ().enabled = (bool)stream.ReceiveNext(); gameObject.GetComponent<VampireGenral> ().enabled = (bool)stream.ReceiveNext(); } }
public void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info){
if(stream.isWriting){
stream.SendNext (isVamp);
stream.SendNext(isHum);
stream.SendNext (gameObject.GetComponent<HumanGenral> ().enabled);
stream.SendNext (gameObject.GetComponent<VampireGenral> ().enabled);
}else{
isVamp = (bool)stream.ReceiveNext ();
isHum = (bool)stream.ReceiveNext ();
gameObject.GetComponent<HumanGenral> ().enabled = (bool)stream.ReceiveNext();
gameObject.GetComponent<VampireGenral> ().enabled = (bool)stream.ReceiveNext();
}
}
Follow this Question
Related Questions
Photon camera problum 0 Answers
Photon Network, synchronize a int over the network. 1 Answer
Rejoin in photon network? 2 Answers