photonView.viewID Issue

Options
Probably another basic silly thing that I'm just missing. I'm writing some code where a player fires a laser and that laser is destroyed when it A. hits another player, and B. hits a planet.

The players and the lasers have photonView's attached.

I call an RPC that says shoot me a laser. Code looks like this:
if(Input.GetButtonDown("Fire1")){
				photonView.RPC("FireLaser", PhotonTargets.All);
			}
[RPC]
	void FireLaser(){
		if(photonView.isMine){
			GameObject laser = PhotonNetwork.Instantiate("laser", 
			                                             new Vector3(transform.localPosition.x, 
			            transform.localPosition.y, 0) + (transform.up * 3f),
			                                             transform.rotation, 0);
			
			laser.GetComponent<Laser>().playerWhoShotMe = photonView.viewID;
		}
	}

where Laser().playerWhoShotMe is an public integer.

In the Laser script I have this code which, on collision with a planet, destroys the laser and let's me know which player's photonview.viewID the laser has on impact:
void OnCollisionEnter2D(Collision2D collider){
		if(collider.gameObject.tag == "planet") {
			Debug.Log(playerWhoShotMe);
			PhotonNetwork.Destroy(gameObject);
		}
	}

Everything works fine when I just have one player. However, when I bring 2+ players into the game. The photonview.viewID shows as '0' and I get this error thrown:
Failed to 'network-remove' GameObject. Client is neither owner nor masterClient taking over for owner who left: View (0)1009 on laser(Clone)
Plus some other gobbildy gook I can post if you want me to.

Of course this effects my player hit detection in the same manner, and I can't register the kills/deaths like I want to.

I know I'm misunderstanding a basic thing about how photonview's are owned, created and destroyed, but I don't know what it is.

This is really slowing me down, so any help would be appreciated. Thanks!

Comments

  • vadim
    Options
    Destroying 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.
    Make sure that you call destroy on planet's owner only. If collision detection triggers on all clients then checking planet's photonView.isMine should be enough.
    Or you can call 'OnCollision' rpc on planet's PhotonView and destroy object there if client is owner.
  • So let's say I have player with a viewID of 2001. The laser is generated by that player, it has to be destroyed by that player as well?

    Right now I have the laser trying to destroy itself with the collision detection...
  • vadim
    Options
    It has to be destroyed by owner. Owner is not necessarily the creator since creator can leave the room.
    That's why you should use 'isMine' property to check if player is owner.