Referencing a raycast hit from a client in Photon

I am having trouble getting the transformOfHit variable in this script to sync with other clients. Basically what I can see is happening is that the transformOfHit variable is reliant on the Ray “hit”. Hit is set equal to Input.mousePosition so i’m pretty sure that the client that has "MyView set to true is using its own mouse position instead it thinks that I am not clicking on anything. If I am wrong please correct me but if not a solution to get a reference to a clients mouse position would be greatly appreciated as I have spent more than 10 hours over 2 days just trying to find a way to get the reference to this so I know what the client has clicked and thus set to its target. I am using this to fight the same enemy which is a scene object as other clients so they can both take its health down and kill it.

I also think it might be worth mentioning that I have 2 cameras on the prefab I am instantiating for each player. If the PhotonView IsMine then I am activating the main camera and if not then I am activating the secondary camera so I can still have a camera that is capable of raycasting based on the mouse position on the client.

public class Raycaster : MonoBehaviourPunCallbacks
{
    public PhotonView PV;

    private Transform camRotate;

    float currentMousePosition;
    float pastMousePosition;

    float screenWidth;
    float screenHeight;

    public Camera cam;
    public Player playerScript;
    public Transform transformOfHit;

    public RaycastHit hit;

    // Start is called before the first frame update
    void Start()
    {
        PV = GetComponent<PhotonView>();
        playerScript = GetComponent<Player>();
        screenWidth = Screen.width;
        screenHeight = Screen.height;
    }

    // Update is called once per frame
    void Update()
    {
        if (!PV.IsMine)
        {
            if (cam == null)
            {
                cam = GetComponentInChildren<Camera>();
            }
            return;
        }

        if (cam == null)
        {
            cam = GetComponentInChildren<Camera>();
        }

        if (Input.GetMouseButtonDown(0))
        {
            // if left button pressed...
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            Debug.Log("This ray hit assigned in this raycaster");
            PV.RPC("RpcSetHit", RpcTarget.OthersBuffered);
          



            if (Physics.Raycast(ray, out hit))
            {
                //Set transforOfHit to whatever the transform of the object that was hit
                PV.RPC("RpcSetTransformOfHit", RpcTarget.AllBuffered);

                // the object identified by hit.transform was clicked
                // do whatever you want
                if (hit.transform.tag == "NPC")
                {
                    print("You clicked an NPC");

                    //If there is already one created then dont create one
                    if (GameObject.Find("ConvoBox(Clone)") == false)
                    {
                        //Flips the convobox after instantiation
                        Quaternion hitQuaternion = hit.transform.rotation * Quaternion.Euler(0, 180f, 0);
                        //Create the prefab
                        Instantiate(Resources.Load("ConvoBox"), new Vector3(hit.transform.position.x,
                            hit.transform.position.y + 5, hit.transform.position.z + 5), hitQuaternion);
                    }
                    else
                        Destroy(GameObject.Find("ConvoBox(Clone)"));
                }

                if (hit.transform.tag == "Teleporter")
                {
                    print("You clicked a Teleporter");

                    if (hit.transform.name == "Flower Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("FlowerSceneTeleporterConvo(Clone)") == false)
                        {

                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("FlowerSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load(""), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Hell Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("HellSceneTeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("HellSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y + 20, hit.transform.position.z + 20), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("HellSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Winter Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("WinterSceneTeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("WinterSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("WinterSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Test Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("TeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Floor 1 Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("Floor1TeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("Floor1TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("Floor1TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }


                }

                if (hit.transform.tag == "Mob")
                {
                    PV.RPC("RpcSetTargetMob", RpcTarget.AllBuffered);

                    print("You clicked on " + transformOfHit.name);
                }

              
            }
        }
    }

    [PunRPC]
    void RpcSetTargetMob()
    {
        playerScript.targetMob = transformOfHit;
    }

    [PunRPC]
    void RpcSetTransformOfHit()
    {
        transformOfHit = hit.transform;
    }

    [PunRPC]
    void RpcSetHit()
    {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        Debug.Log("This ray hit assigned in some1 elses raycaster");
    }
}

So I’m still not sure what it was that was preventing me from passing a reference from the clients RaycastHit directly but I was able to workaround this by referencing the PhotonId of the RaycastHit object (targeted enemy) on the Clients computer and passing that with OnPhotonSerializeView as an int then comparing that id to get the object for the rpc. This was a very complicated issue that took me 3 days to find a workaround for. I will post the code if any1 wants to learn how to find targeted objects from clients using a referenced PhotonId.

public class Raycaster : MonoBehaviourPunCallbacks, IPunObservable
{
    public PhotonView PV;

    private Transform camRotate;

    float currentMousePosition;
    float pastMousePosition;

    float screenWidth;
    float screenHeight;

    public int idOfTarget = -1;

    public Camera cam;
    public Player playerScript;
    public Player targetPlayerScript;
    public GameObject targetEnemyObject;

    public Transform transformOfHit;

    public RaycastHit targetHit;

    public Enemy targetEnemyScript;

    // Start is called before the first frame update
    void Start()
    {
        PV = GetComponent<PhotonView>();
        playerScript = GetComponent<Player>();
        screenWidth = Screen.width;
        screenHeight = Screen.height;
    }

    // Update is called once per frame
    void Update()
    {
        if (!PV.IsMine)
        {
            return;
        }

        if (cam == null)
        {
            cam = GetComponentInChildren<Camera>();
        }


        if (Input.GetMouseButtonDown(0))
        {

            // if left button pressed...
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                targetHit = hit;

                //Set transforOfHit to whatever the transform of the object that was hit
                transformOfHit = hit.transform;

                if (transformOfHit != null)
                {
                    if (transformOfHit.gameObject.GetPhotonView() != null)
                    {
                        idOfTarget = transformOfHit.gameObject.GetPhotonView().InstantiationId;
                    }

                }





                // the object identified by hit.transform was clicked
                // do whatever you want
                if (hit.transform.tag == "NPC")
                {
                    print("You clicked an NPC");

                    //If there is already one created then dont create one
                    if (GameObject.Find("ConvoBox(Clone)") == false)
                    {
                        //Flips the convobox after instantiation
                        Quaternion hitQuaternion = hit.transform.rotation * Quaternion.Euler(0, 180f, 0);
                        //Create the prefab
                        Instantiate(Resources.Load("ConvoBox"), new Vector3(hit.transform.position.x,
                            hit.transform.position.y + 5, hit.transform.position.z + 5), hitQuaternion);
                    }
                    else
                        Destroy(GameObject.Find("ConvoBox(Clone)"));
                }

                if (hit.transform.tag == "Teleporter")
                {
                    print("You clicked a Teleporter");

                    if (hit.transform.name == "Flower Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("FlowerSceneTeleporterConvo(Clone)") == false)
                        {

                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("FlowerSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load(""), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Hell Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("HellSceneTeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("HellSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y + 20, hit.transform.position.z + 20), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("HellSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Winter Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("WinterSceneTeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("WinterSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("WinterSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Test Scene Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("TeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }
                    if (hit.transform.name == "Floor 1 Teleporter")
                    {
                        //If there is already one created then dont create one
                        if (GameObject.Find("Floor1TeleporterConvo(Clone)") == false)
                        {
                            Quaternion hitQuaternion = transform.rotation;
                            //Create the prefab
                            Instantiate(Resources.Load("Floor1TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
                                hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);

                            //Create the Prefab
                            //Instantiate(Resources.Load("Floor1TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
                        }
                    }


                }


            }


        }

        PV.RPC("RpcSetTarget", RpcTarget.AllBuffered);
    }

    void FixedUpdate()
    {
        if (idOfTarget != -1)
        {
            targetEnemyObject = PhotonView.Find(idOfTarget).gameObject;
        }

    }

    [PunRPC]
    void RpcSetTarget()
    {
        if (targetEnemyObject != null)
        {
            if (targetEnemyObject.transform.tag == "Mob")
            {
                playerScript.targetMob = targetEnemyObject.transform;

                print("You clicked on " + targetEnemyObject.name);

                targetEnemyScript = playerScript.targetMob.gameObject.GetComponent<Enemy>();

                if (targetEnemyScript.health <= 0)
                {
                    idOfTarget = -1;
                }
            }
            else if (targetEnemyObject.transform.tag == "Player")
            {
                playerScript.targetMob = targetEnemyObject.transform;
                targetPlayerScript = targetEnemyObject.GetComponent<Player>();
               
            }
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(idOfTarget);
        }
        else
        {
            idOfTarget = (int)stream.ReceiveNext();
        }
    }
}