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

2 Answers

2

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"?

Thank you! I will be trying this asap! I will post back and let you know my progress. THANK YOU AGAIN for looking into this!

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?

I've been using the statement if(!PhotonView.isMine) myPhotonView.renderer.enable = weild; however... It feels like that's the incorrect thing... is there a way to say if(keypress.ismine) myPhotonview.RPC(...); and later if(prefab.isMine) myPhotonview.prefab.sheild.renderer.enabled = weild; if(prefab.sentlastRPC.sheild.renderer.enabled = haschanged) myPhotonview.prefab.sheild.renderer.enabled = prefab.sentlastRPC.weild;

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!