NullReferenceException

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

NullReferenceException: Object reference not set to an instance of an object

RobertB
2018-05-03 19:48:07

Well, after searching the Internet for this answer, I can not figure out how this is happening. I have seen forums discussing that you need to Instantiate the object, but I can not get my head wrapped around what the code needs to look like.
The idea is this: The player wants to launch a UAV (drone), and see the camera, that is on the drone, displayed on the monitor he is looking at, on the table, so that he can fly the drone around...
This is what I have at this point


using System.Collections;

using System.Collections.Generic;
using UnityEngine;

public class DroneMonitor_Trigger_Script : Photon.MonoBehaviour
{
public GameObject Drone;
public float Speed;
public bool HaveWeEnteredTrigger = false;
public Vector3 dir;

//=========================================================================  
[PunRPC]  
public void MoveDrone(Vector3 direction)  
{  
    Drone.transform.Translate(Vector3.up * Time.deltaTime * Speed);  
}

//=========================================================================  
public void OnTriggerEnter(Collider other)  
{  
    if(other.tag == "Player")  
    {  
        HaveWeEnteredTrigger = true;  
          
    }  
}

//=========================================================================  
  
// Update is called once per frame  
void Update ()  
{  
    if (Input.GetKey(KeyCode.Insert))  
    {  
        if (HaveWeEnteredTrigger == true)  
        {  
            dir = Vector3.up;  
            photonView.RPC("MoveDrone", PhotonTargets.MasterClient, dir);   
            // The above line is what is generating the error...  
            // NULLREFERENCEEXCEPTION: Object reference not set to an instance of an object.  
            // DroneMonitor_Trigger_Script.Update() (at Assets/DroneMonitor_Trigger_Script.cs:39)  
        }  
    }  
<img src="http://www.carpatheous.com/DroneWithCamera.png" alt="" />  

}
}

## Comments

RobertB
2018-05-03 19:51:47

Why does this not post the code in a formatted way? I used the <code="CodeInLine"> but it does not format it correctly. What is wrong with this website?

[Deleted User]
2018-05-08 08:14:19

Hi @RobertB,

do you have an actual reference to the attached PhotonView component? You can do this for example in the Awake or Start function by using photonView = GetComponent();.

Back to top