-1

I'm making a 2D shooter game, and I'm running into some errors.

Received RPC "DestroyRPC" for viewID 2028 but this PhotonView does not exist! Was remote PV. Owner called. By: #02 'davdv' Maybe GO was destroyed but RPC not cleaned up.

Received OnSerialization for view ID 2004. We have no such PhotonView! Ignore this if you're joining or leaving a room. State: Joined

These two errors continue to occur while the bullet is firing.

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviourPunCallbacks
{
    private Vector3 dir;
    public Camera cam;
    public GameStart GS;
    public float bulletSpeed = 10f; // 총알의 속도
    public GameObject bulletPrefab; // 총알의 프리팹
    // Start is called before the first frame update

    private void Awake()
    {
        GS = FindObjectOfType<GameStart>();
    }
    void Start()
    {
        cam = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)&&photonView.IsMine)
        {
            if (GS.Isshot)
            {
                dir = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -cam.transform.position.z));
                photonView.RPC("ShootBullet", RpcTarget.AllBuffered, dir);
            }
        }
    }

    [PunRPC]
    void ShootBullet(Vector3 dir)
    {
        if (photonView.IsMine)
        {
            // 총알 생성 위치 계산
            Vector3 bulletSpawnPosition = transform.position;

            GameObject bullet = PhotonNetwork.Instantiate("Bullet", bulletSpawnPosition, Quaternion.identity);
            bullet.GetComponent<Rigidbody2D>().velocity = (dir - transform.position).normalized * bulletSpeed;
        }
    }
}
[weapon.cs]
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviourPunCallbacks
{
    public float bulletDamage;
    public PhotonView PV;


    void Start() => Destroy(gameObject, 3.5f);


    void OnTriggerEnter2D(Collider2D hitInfo)
    {

        if(hitInfo.tag=="Border") this.PV.RPC("DestroyRPC", RpcTarget.AllBuffered);
        PhotonView target = hitInfo.gameObject.GetComponent<PhotonView>();
        if (target != null && (!target.IsMine))
        {
            if (target.tag == "Player"&&!PV.IsMine&&hitInfo.GetComponent<PhotonView>().IsMine)
            {
                target.RPC("reduceHealth", RpcTarget.All, bulletDamage);
                this.PV.RPC("DestroyRPC", RpcTarget.AllBuffered);
            }
        }
    }

    [PunRPC]
    void DestroyRPC()
    {
        Destroy(this.gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using TMPro;
using System.Xml.Serialization;
using Cinemachine;
using System;
using System.Runtime.CompilerServices;

public class Player : MonoBehaviourPunCallbacks, IPunObservable
{
    public GameObject CamRange;
    public Transform diepoint;
    public float moveSpeed = 5f;  // 플레이어의 이동 속도
    public TextMeshProUGUI NickNameText;
    public TextMeshProUGUI KillText;
    public int killNum;
    public int randrespawn;
    public bool immortal;

    public GameStart GS;
    private Rigidbody2D RB;
    private PhotonView PV;
    private Quaternion networkRotation;
    Vector3 curPos;

    public Camera cam;


    void Awake()
    {
        CamRange = GameObject.Find("CAMRANGE");
        GS=FindObjectOfType<GameStart>();
        immortal = false;
        RB = GetComponent<Rigidbody2D>();
        PV = GetComponent<PhotonView>();
        NickNameText.text = PV.IsMine ? PhotonNetwork.NickName : PV.Owner.NickName;
        NickNameText.color = PV.IsMine ? Color.green : Color.red;
    }

    void Start()
    {
        diepoint = GameObject.Find("diepoint").transform;
        cam = Camera.main;
    }


    public void TeleportToTarget(Vector3 targetPosition)
    {
        transform.position = targetPosition;
    }

    private void Update()
    {
        KillText.text = $"{killNum}kill";
        if (photonView.IsMine)
        {
            float axisx = Input.GetAxisRaw("Horizontal");
            float axisy = Input.GetAxisRaw("Vertical");
            RB.velocity = new Vector2(4 * axisx, 4 * axisy);
            var CM = GameObject.Find("CMCamera").GetComponent<CinemachineVirtualCamera>();
            CM.Follow = transform;
            CM.LookAt = transform;

            // 마우스 클릭 시 발사 처리
        }
        else if ((transform.position - curPos).sqrMagnitude >= 100) transform.position = curPos;
        else transform.position = Vector3.Lerp(transform.position, curPos, Time.deltaTime * 10);
    }

    public void Hit()
    {
        if (!immortal)
        {
            CamRange.SetActive(false);
            immortal = true;
            transform.Translate(diepoint.position);
            StartCoroutine(WaitCoroutine());
        }
    }
    [PunRPC]
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            // 자신의 위치 및 회전 정보를 네트워크로 전송
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        }
        else
        {
            // 네트워크로부터 위치 및 회전 정보를 수신
            curPos = (Vector3)stream.ReceiveNext();
            networkRotation = (Quaternion)stream.ReceiveNext();
        }
    }
    IEnumerator WaitCoroutine()
    {
        yield return new WaitForSeconds(5.0f);
        CamRange.SetActive(true);
        transform.position = Vector3.zero;
        Invoke("Immortal", 2.0f);
    }

    private void Immortal()
    {
        immortal = false;
    }

}

bullet.cs->bullet

player.cs /weapon.cs->player

I modified the code to solve the error and looked at the related code.

https://goraniunity2d.blogspot.com/2019/08/2d-pun2.html

https://github.com/glassbrickstudio/2D-Multiplayer-Shooter

kennarddh
  • 2,186
  • 2
  • 6
  • 21
이관우
  • 3
  • 3
  • Why are you sending `photonView.RPC("ShootBullet", RpcTarget.AllBuffered, dir);` as an RPC if it is anyway only supposed to be handled by your local client (`if(photonView.isMine)`)? And then `Destroy(this.gameObject);` should also contain a check for `phtonView.isMine` and then use `PhotonNetwork.Destroy` in order to destroy network wide! – derHugo Aug 07 '23 at 10:25
  • There are also more edge cases to solve (see [`PhotonNetwork.Destroy`](https://doc-api.photonengine.com/en/pun/current/class_photon_1_1_pun_1_1_photon_network.html#a75f0f61ec71729b782a9da6bbad86e97) -> `The GameObject must be under this client's control: Instantiated and owned by this client. Instantiated objects of players who left the room are controlled by the Master Client. Room-owned game objects are controlled by the Master Client. GameObject can be destroyed while client is not in a room. ` – derHugo Aug 07 '23 at 10:28
  • I'm not sure if I understood you well, I changed the command Destroy(this.gameObject) ->PhotonNetwork.Destroy(gameObject), but the error keeps popping up and "Failed to 'network-remove' GameObject. Client is neither owner nor MasterClient taking over for owner who left: View 1007 on Bullet(Clone) UnityEngine.Debug:LogError (object)" this error occured. How can I fix it? – 이관우 Aug 11 '23 at 11:27
  • sorry for my short English. – 이관우 Aug 11 '23 at 11:29
  • As said in the citation above .. in order to be allowed to destroy an object network-wide you have to either be the owner, or for objects that don't have an owner (belong to the scene) you have to be master client => Whenever you destroy something you have to check the owner first => if there is one send an RPC to that owner that he should destroy the object network wide - if there is no owner instead tell the master client to delete the object network wide ... you wouldn't send those RPC to **all** clients but rather expect the network destroy already handles it correctly – derHugo Aug 11 '23 at 13:14

0 Answers0