- Home /
Photon Prefab specific bool
So, I'm trying to send render states of specific objects equipped to my player. Here is my current code: using UnityEngine; using System.Collections;
public class sheildWeild : MonoBehaviour {
private bool weild;
public Renderer rens;
//GameObject SHEILD;
private PhotonView myPhotonView;
public static string setRenderer = "SetRenderer";
// Use this for initialization
void Start () {
myPhotonView = this.GetComponent<PhotonView>();
weild=true;
Debug.Log (weild);
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.BackQuote))
{
if(weild==false)
{
this.myPhotonView.RPC("SetRenderer", PhotonTargets.AllBuffered, true);
weild=true;
}
else
{
this.myPhotonView.RPC("SetRenderer", PhotonTargets.AllBuffered, false);
weild=false;
}
}
Debug.Log (weild);
}
[RPC]
void SetRenderer(bool weild){
this.renderer.enabled=weild;
}
}
This code allows all clients to see when a player's shield is wielded or not. The problem is, all players on their client weild/unweild their shield. Only the client's player should change render state.
the picture above displays the left client having a sheathed status, while the client to the right has wielded status. As the left client was the last to affect the status of the shield's renderer.enabled property, he has not only affected his own shield on the other client's view, but all shields in game.
How can I make the Boolean weild only affect the active client's objects?
I was attempting to play with public Renderer rens;
Now i'm thinking maybe I need to use public Renderer rens[];
and possibly pull the playerID into the array for location?
I'm not sure how to pull the playerID or if this would even work
Answer by CiberX15 · Dec 04, 2013 at 01:43 AM
I did not write this but I believe it is what you are looking for. What it does is check if the object in question is remote or local and enables and disables scripts as necessary. To make it work simply fill the arrays with the scripts you want to be enabled/disabled and it should do the rest.
using UnityEngine;
using System.Collections;
public class PlayerNetworkInit : MonoBehaviour
{
[SerializeField] Behaviour[] behavioursEnabledOnLocalClientsOnly;
[SerializeField] Behaviour[] behavioursEnabledOnRemoteClientsOnly;
[SerializeField] GameObject[] gameObjectsEnabledOnLocalClientsOnly;
[SerializeField] GameObject[] gameObjectsEnabledOnRemoteClientsOnly;
void OnNetworkInstantiate( NetworkMessageInfo msg )
{
if( !networkView.isMine )
{
name += "(remote)";
}
foreach( Behaviour behaviour in behavioursEnabledOnLocalClientsOnly )
{
behaviour.enabled = networkView.isMine;
}
foreach( Behaviour behaviour in behavioursEnabledOnRemoteClientsOnly )
{
behaviour.enabled = !networkView.isMine;
}
foreach( GameObject go in gameObjectsEnabledOnLocalClientsOnly )
{
go.SetActive(networkView.isMine);
}
foreach( GameObject go in gameObjectsEnabledOnRemoteClientsOnly )
{
go.SetActive(!networkView.isMine);
}
}
}
I think this question got 'vanished' by superiors... I don't quite understand your directions
This should be equipped to my prefab? or the object i'm changing states on?
behaviorsEnabledOnLocalClientsOnly should be changed to "sheildweild"?
It should be on the prefab, added as a script. In the editor it will have four different arrays that can store scripts. Drag the scripts that are already on the prefab onto the the monobehavor arrays.
On the client side what happens is you get several identical entity's all looking for the same inputs. What this does is check if the object originated on the particular client and if not stops those scripts from executing.
$$anonymous$$ind you this means that if there is part of your script that should be firing regardless of whether it is local or remote, then you will either need to move it to a separate script or canabalise the above script.
networkView.is$$anonymous$$ine is the major workhorse of the script. it returns a bool based on wither its gameobject is local or remote.
Thank you! I will be trying this asap!
I will post back and let you know my progress. THAN$$anonymous$$ YOU AGAIN for looking into this!
Okay... I got the local set up and it appears to be affecting the opposite player on the other player's client.
on my client my character changes state, but on the other client their character changes state on $$anonymous$$Y keypress
edit: SS not showing?
This only appears to be giving both clients lag, all protocalls follow the previous setups :\
I've tried 3 different script influences such as renderer.enabled = true;
if(photonView.is$$anonymous$$ine)
myphotonview.renderer.enabled = true;
if(!photonview.is$$anonymous$$ine)
this.renderer.enabled = true;
I'm beginning to think the keypress has something to do with it
Well the other client should not be able to detect your remote key-presses. However if you have a network view set to sync the scripts it may be detecting it on your client then syncing the remote script. I think you can change that in the network view?
Answer by KuPAfoo · Dec 05, 2013 at 10:41 PM
I feel dumb... the answer was to make my Update() private and also my setRenderer() private. Well... I learned a lot of stuff in this 80 lines or so of code. I hope the papertrail can help someone else!
Your answer
Follow this Question
Related Questions
Photon shot collision 1 Answer
Server receives RPCs after RemoveRPCs and Destroy 0 Answers
Assign Prefab to script as Code? 2 Answers
Unity networking tutorial? 6 Answers