Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Third Party Changing GameObject Tag and Layer on Photon during Runtime

Discussion in 'Multiplayer' started by echo2045, Jan 6, 2021.

  1. echo2045

    echo2045

    Joined:
    Nov 21, 2020
    Posts:
    14
    I am trying to change the tag and the layer of a gameobject during runtime. However, the tag and layer only changes for the client that instantiated the gameobject which makes me think the change is only happeneing locally. I am using RPC and don't have much experience with it. Based on online research I found out Photon only ;ets you change the ViewID of an object not the object itself.

    TLDR
    I need the gameobject to change it's layer and tag in runtime on photon Pun 2. The code I am using now is below and it only changes the Tag and layer for the client that instantiates the gameobject. I've bolded and underlined the code I think is important.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. using Photon.Pun;
    7. using Photon.Realtime;
    8. using System.IO;
    9. using Cinemachine;
    10.  
    11.  
    12.  
    13. public class KillMechanic : MonoBehaviour
    14. {
    15.  
    16.     Rigidbody myRB;
    17.     Transform myAvatar;
    18.     Animator myAnim;
    19.  
    20.     [SerializeField] public bool isImpostor;
    21.     [SerializeField] InputAction KILL;
    22.  
    23.     //float killInput;
    24.  
    25.     public List<KillMechanic> targets;
    26.     [SerializeField] Collider myCollider;
    27.  
    28.     public bool isDead;
    29.  
    30.     PhotonView PV;
    31.  
    32.     [SerializeField] public GameObject BodyPrefab;
    33.     //[SerializeField] public GameObject GhostPrefab;
    34.     //[SerializeField] public GameObject BodyPrefab;
    35.    
    36.     public GameObject Player;
    37.     public Camera PerspectiveCamera;
    38.     //public GameObject Ghost;
    39.  
    40.  
    41.  
    42.      // Start is called before the first frame update
    43.     void Start()
    44.     {
    45.  
    46.         PV = GetComponent<PhotonView>();
    47.         if(!PV.IsMine)
    48.         {
    49.             return;
    50.         }
    51.         else{
    52.  
    53.         myAnim = GetComponent<Animator>();
    54.         targets = new List<KillMechanic>();
    55.         //targets = null;
    56.         myRB = GetComponent<Rigidbody>();
    57.         isDead = false;
    58.  
    59.         }
    60.          
    61.     }
    62.  
    63.  
    64.     private void Awake() {
    65.         KILL.performed += KillTarget;
    66.     }
    67.  
    68.  
    69.     private void OnEnable() {
    70.         KILL.Enable();
    71.     }
    72.  
    73.     private void OnDisable() {
    74.         KILL.Disable();
    75.     }
    76.  
    77.     void SetRole (bool newRole) {
    78.         isImpostor = newRole;
    79.     }
    80.  
    81.     private void OnTriggerEnter(Collider other){
    82.        AddList(other);
    83.     }
    84.  
    85.     private void OnTriggerExit (Collider other){
    86.        RemoveList(other);
    87.     }
    88.  
    89.     void AddList(Collider other){
    90.  
    91.         if(other.gameObject.tag == "Player"){
    92.             KillMechanic tempTarget = other.gameObject.GetComponent<KillMechanic>();
    93.            /* if(isImpostor){
    94.                 if(tempTarget.isImpostor)
    95.                 return;
    96.                 else
    97.                 {
    98.                     */
    99.                     targets.Add(tempTarget);
    100.                     //Debug.Log(target.name);
    101.                 //}
    102.             //}
    103.  
    104.         }
    105.  
    106.  
    107.        
    108.     }
    109.  
    110.     void RemoveList(Collider other){
    111.  
    112.          if (other.gameObject.tag == "Player"){
    113.             KillMechanic tempTarget = other.GetComponent<KillMechanic>();
    114.             if (targets.Contains(tempTarget)){
    115.                 targets.Remove(tempTarget);
    116.             }
    117.         }
    118.  
    119.        
    120.     }
    121.  
    122.     void KillTarget (InputAction.CallbackContext context){
    123.  
    124.         if(!PV.IsMine)
    125.         return;
    126.  
    127.       //  if(!isImpostor)
    128.         //return;
    129.        
    130.         if (context.phase == InputActionPhase.Performed){
    131.             if(targets.Count == 0 )
    132.             return;
    133.             else
    134.             {
    135.                 if(targets[targets.Count - 1].isDead){
    136.                     return;
    137.                 }
    138.                 else{
    139.                // transform.position = target.transform.position;
    140.                // targets[targets.Count - 1].Die();
    141.  
    142.               [B][U] targets[targets.Count - 1].PV.RPC("RPC_Kill",RpcTarget.All);[/U][/B]
    143.                
    144.                 targets.RemoveAt(targets.Count - 1);
    145.                 }
    146.                
    147.  
    148.             }
    149.         }
    150.     }
    151.  
    152.  
    153.  [B][U]   [PunRPC]
    154.     void RPC_Kill() {
    155.         Die();
    156.     }
    157.  
    158.     public void Die(){
    159.  
    160.         if(!PV.IsMine)
    161.         return;
    162.  
    163.  
    164.         isDead = true;
    165.  
    166.  
    167.         makeGhost();
    168.         makeBody();
    169.  
    170.  
    171.      
    172.  
    173.  
    174.     }[/U][/B]
    175.  
    176.    
    177.  
    178.     // Update is called once per frame
    179.     void Update()
    180.     {
    181.        // if (!PV.IsMine)
    182.        // return;
    183.        
    184.     }
    185.  
    186.  
    187.    [B][U] void makeGhost(){
    188.  
    189.  
    190. /*        myAnim.SetBool("IsDead", isDead);*/
    191.         Player.layer = LayerMask.NameToLayer("Ghost");
    192.         Destroy(Player.GetComponent<Rigidbody>());
    193.         myCollider.enabled = false;
    194.         Player.tag = "Ghost";
    195. /*        PerspectiveCamera.cullingMask |= 1 << LayerMask.NameToLayer("Ghost");*/
    196.         Player.GetComponent<PlayerMovementController>().enabled = false;
    197.         Player.GetComponent<GhostController>().enabled = true;
    198.  
    199.        
    200.     }[/U][/B]
    201.  
    202.     void makeBody(){
    203.  
    204.        
    205.         Vector3 bodyPosition = new Vector3 (0,1f,0);
    206.         //Quaternion bodyRotation = new Quaternion(90f,0,0,0);
    207.  
    208.         //body tempBody = Instantiate(BodyPrefab, transform.position, transform.rotation).GetComponent<body>();
    209.         //body tempBody = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","body"), transform.position + bodyPosition, transform.rotation).GetComponent<body>();
    210.         PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","body"), transform.position + bodyPosition, transform.rotation);
    211.  
    212.  
    213.  
    214.     }
    215. }
    216.  
    217.  
    218.  
     
  2. asadimam

    asadimam

    Joined:
    Oct 19, 2022
    Posts:
    1
    did you find any solution