Absolutely beginner | photonview.ismine - Object reference not set to an instance of an object
My game was playing without any error on single player, then i decided to go hard and build my game as multiplayer.
I will try to explain what happens at the moment:
I have one prefab called Player and it instantiate fine. I would need to control almost everything assigned to the player in photon too, such as ammo, rifle, health, Canvas, armor, movement, mouse movement, shoot etc etc.
So i start with weapons. here is my code below; when game starts i dont get an error, but if i activate weapon im getting error message. Where ever i add photonview.ismine i am getting an error, surely i am not doing it right, however i dont know how to make it right too...
can anyone help me to understand how to set it up? thanks
I will try to explain what happens at the moment:
I have one prefab called Player and it instantiate fine. I would need to control almost everything assigned to the player in photon too, such as ammo, rifle, health, Canvas, armor, movement, mouse movement, shoot etc etc.
So i start with weapons. here is my code below; when game starts i dont get an error, but if i activate weapon im getting error message. Where ever i add photonview.ismine i am getting an error, surely i am not doing it right, however i dont know how to make it right too...
can anyone help me to understand how to set it up? thanks
using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; public class WeaponSwitch : MonoBehaviourPunCallbacks { [SerializeField] int currentWeapon = 0; private PhotonView PV; void Start() { SetWeaponActive(); PV = GetComponent<PhotonView>(); } public void Update() { int previousWeapon = currentWeapon; ProcessKeyInput(); ProcessScrollWheel(); if(previousWeapon != currentWeapon) { SetWeaponActive(); } } public void ProcessScrollWheel() { if (Input.GetAxis("Mouse ScrollWheel") < 0 && PV.IsMine) { if (currentWeapon >= transform.childCount - 1) { currentWeapon--; } else { currentWeapon++; } } if (Input.GetAxis("Mouse ScrollWheel") > 0 && PV.IsMine) { if (currentWeapon <= 0) { currentWeapon = transform.childCount - 1; } else { currentWeapon--; } } } public void ProcessKeyInput() { if(Input.GetKeyDown(KeyCode.Alpha1) && PV.IsMine) { currentWeapon = 1; } if (Input.GetKeyDown(KeyCode.Alpha2) && PV.IsMine) { currentWeapon = 2; } if (Input.GetKeyDown(KeyCode.Alpha3) && PV.IsMine) { currentWeapon = 3; } if (Input.GetKeyDown(KeyCode.G) && PV.IsMine) { currentWeapon = 4; } if (Input.GetKeyDown(KeyCode.Alpha4) && PV.IsMine) { currentWeapon = 4; } if (Input.GetKeyDown(KeyCode.Alpha5) && PV.IsMine) { currentWeapon = 5; } if (Input.GetKeyDown(KeyCode.Alpha6) && PV.IsMine) { currentWeapon = 6; } } public void SetWeaponActive() { int weaponIndex = 0; foreach (Transform weapon in transform) { if(weaponIndex == currentWeapon) { weapon.gameObject.SetActive(true); } else { weapon.gameObject.SetActive(false); } weaponIndex++; } } }
0
Comments
-
0