[PUN2] Object Active / Not Active

I wondered if I could get some assistance on PUN 2.

I have a script that means when the player presses "D" the object will change (One goes inactive, the other goes active)
public bool right;
 public bool center;
 
 public GameObject rightobj;
 public GameObject centerobj;
Then on update I have this.
if (Input.GetKeyDown(KeyCode.D))
             {
 
             if (center == true)
             {
                 if (photonView.IsMine)
                 {
                    
                     centerobj.SetActive(false);
                     rightobj.SetActive(true);
                     right = true;
                     center = false;
                    }
                else
                  {
                    centerobj.SetActive(false);
                     rightobj.SetActive(true);
                 }
 
             }
I have also tried adding this to the bottom of the script.
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.IsWriting)
         {
             stream.SendNext(rightobj.activeSelf);
             stream.SendNext(centerobj.activeSelf);
         }
         else if (stream.IsReading)
         {
           
             rightobj.SetActive((bool)stream.ReceiveNext());
             centerobj.SetActive((bool)stream.ReceiveNext());
         }
     }
 }
I have literally no luck. When the player presses "D" the object will change locally but nothing will happen on the "Other Player" end.

Could someone advise a simpler solution if possible?

Thankyou very much

Comments

  • S_OliverS_Oliver ✭✭✭
    Hi,
    dont forget that the PhotonView has to oberseve your script.
    Here an Example.
    using Photon.Pun;
    using UnityEngine;
    
    [RequireComponent(typeof(PhotonView))]
    public class PetHandler : MonoBehaviour, IPunObservable
    {
    	[SerializeField] private GameObject m_objectA;
    	[SerializeField] private GameObject m_objectB;
    	private PhotonView m_view;
    
    	private void Start()
    	{
    		m_view = GetComponent<PhotonView>();
    		AddObservable();
    	}
    
    	private void AddObservable()
    	{
    		if (!m_view.ObservedComponents.Contains(this))
    		{
    			m_view.ObservedComponents.Add(this);
    		}
    	}
    
    	private void Update()
    	{
    		if (!m_view.IsMine) return;
    
    		if (Input.GetKeyDown(KeyCode.Space))
    		{
    			m_objectA.SetActive(!m_objectA.activeSelf);
    			m_objectB.SetActive(!m_objectB.activeSelf);
    		}
    	}
    
    	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    	{
    		if (stream.IsWriting)
    		{
    			stream.SendNext(m_objectA.activeSelf);
    			stream.SendNext(m_objectB.activeSelf);
    		}
    		else
    		{
    			m_objectA.SetActive((bool) stream.ReceiveNext());
    			m_objectB.SetActive((bool) stream.ReceiveNext());
    		}
    	}
    }
    
Sign In or Register to comment.