Tutorial section 5 - Object reference not set to an instance of an object

Hi all,

I am currently working on section 5 of the PUN basics tutorial (https://doc.photonengine.com/en-us/pun/v2/demos-and-tutorials/pun-basics-tutorial/player-prefab), and everything went rather smooth up until now.

In this section, you are asked to create a PlayerManager script and attach it to your My Robot Kyle prefab. My code is as follows, and please see a screenshot of my UI as well (showing My Robot Kyle prefab, that it is in the Resources folder, etc.). The error I get is: "NullReferenceException: Object reference not set to an instance of an object
Com.MyCompany.MyGame.PlayerManager.Update () (at Assets/Scripts/PlayerManager.cs:55)"


Could you please help me out?

using UnityEngine;
using UnityEngine.EventSystems;
using Photon.Pun;

using System.Collections;

namespace Com.MyCompany.MyGame
{
    /// <summary>
    /// Player manager.
    /// Handles fire Input and Beams.
    /// </summary>
    public class PlayerManager : MonoBehaviourPunCallbacks
    {
        #region Private Fields

        [Tooltip("The Beams GameObject to control")]
        [SerializeField]
        private GameObject beams;
        //True, when the user is firing
        bool IsFiring;
        #endregion

        #region Public Fields

        [Tooltip("The current Health of our player")]
        public float Health = 1f;

        #endregion

        #region MonoBehaviour CallBacks

        /// <summary>
        /// MonoBehaviour method called on GameObject by Unity during early initialization phase.
        /// </summary>
        void Awake()
        {
            if (beams == null)
            {
                Debug.LogError("<Color=Red><a>Missing</a></Color> Beams Reference.", this);
            }
            else
            {
                beams.SetActive(false);
            }
        }

        /// <summary>
        /// MonoBehaviour method called on GameObject by Unity on every frame.
        /// </summary>
        void Update()
        {

            // we only process Inputs if we are the local player
            if (photonView.IsMine)
            {
                ProcessInputs();
            }

            // trigger Beams active state
            if (beams != null && IsFiring != beams.activeSelf)
            {
                beams.SetActive(IsFiring);
            }
        }

        /// <summary>
        /// MonoBehaviour method called when the Collider 'other' enters the trigger.
        /// Affect Health of the Player if the collider is a beam
        /// Note: when jumping and firing at the same, you'll find that the player's own beam intersects with itself
        /// One could move the collider further away to prevent this or check if the beam belongs to the player.
        /// </summary>
        void OnTriggerEnter(Collider other)
        {
            if (!photonView.IsMine)
            {
                return;
            }
            // We are only interested in Beamers
            // we should be using tags but for the sake of distribution, let's simply check by name.
            if (!other.name.Contains("Beam"))
            {
                return;
            }
            Health -= 0.1f;
        }
        /// <summary>
        /// MonoBehaviour method called once per frame for every Collider 'other' that is touching the trigger.
        /// We're going to affect health while the beams are touching the player
        /// </summary>
        /// <param name="other">Other.</param>
        void OnTriggerStay(Collider other)
        {
            // we dont' do anything if we are not the local player.
            if (!photonView.IsMine)
            {
                return;
            }
            // We are only interested in Beamers
            // we should be using tags but for the sake of distribution, let's simply check by name.
            if (!other.name.Contains("Beam"))
            {
                return;
            }
            // we slowly affect health when beam is constantly hitting us, so player has to move to prevent death.
            Health -= 0.1f * Time.deltaTime;
        }

        #endregion

        #region Custom

        /// <summary>
        /// Processes the inputs. Maintain a flag representing when the user is pressing Fire.
        /// </summary>
        void ProcessInputs()
        {
            if (Input.GetButtonDown("Fire1"))
            {
                if (!IsFiring)
                {
                    IsFiring = true;
                }
            }
            if (Input.GetButtonUp("Fire1"))
            {
                if (IsFiring)
                {
                    IsFiring = false;
                }
            }
        }

        #endregion
    }
}

Comments

  • FYI - is refers to the following section in the code:

    void Update()
    {

    // we only process Inputs if we are the local player
    if (photonView.IsMine)
    {
    ProcessInputs();
  • And here is the picture, it did not show up when I attached is to the original post.

    https://imgur.com/a/ZrODYIH
  • I have the same problem here as the host! Did you manage to solve it? Anyone wanna help us here?
  • I solved it

    What I did is change the MonoBehaviour to MonoBehaviourPun
  • us photoView tho not (Photo.View)
  • I think the problem is that the robot doesn't have a PhotonView component.
    It should be pointed out in the tutorial that you have to add a PhotonView but maybe it's not there.

    We will check out the tutorial for that detail.
    Definitely add a PhotonView to the prefab, so it's present when you instantiate the object.
  • Adding a PhotonView component may be necessary, but that doesn't change the fact that the script doesn't know what a photonView is.
  • I added:
    using Photon.Pun;
    and changed to:
    public class PlayerManager : MonoBehaviourPun
    as stated above.
  • We have updated the tutorial slightly to address this issue:

    "PhotonView Component" § was moved from part "7 - Player Networking" to part "5 - Building the Player" before the section about "PlayerManager".

    PlayerManager extends MonoBehaviourPunCallbacks which extends MonoBehaviourPun.
  • @JohnTube, maybe I missed it, but I think there is supposed to be a private field called "photonView" and that on Start it should be assigned to the return value of GetComponent<PhotonView>(). In the "health" section you tell us to check for whether "(!photonView.IsMine)" but we've never declared a lower-case "p" field "photonView" to reference here. Or am I misreading what I'm supposed to be doing?
  • Hi @tetripod,

    I replied here.
Sign In or Register to comment.