Home c# NULLReferenceException: Object Reference Not Set to An Instance of An Object in...

NULLReferenceException: Object Reference Not Set to An Instance of An Object in Unity

Author

Date

Category

Trying to make the basis of the VR game to the tutorial, but then the problem surfaced
NULLREFERENCEEXCEPTION: Object Reference Not Set to An Instance of An Object
Handpresence.update () (at assets / handpresence.cs: 56)
And because of this problem, my controllers and “hands” are displayed on controllers. I tried to remove some parts of the code and after removing the code in Update, nothing changes.

Here the code itself (marked 56 row) and here is the link to the video of a person with a timecode https: // Youtu .be / vdt0zmcggtq? t = 912

using system.collections;
Using System.Collections.Genic;
using unityengine;
using unityengine.xr;
Public Class Handpresence: Monobehaviour
{
  Public Bool ShowController = False;
  Public InputDeviceCharacteristics Controllercharacteristics;
  Public List & LT; GameObject & GT; ControllerPrefabs;
  Public GameObject HandmodelPrefab;
  Private InputDevice TargetDevice;
  Private GameObject SpawnedController;
  Private GameObject SpawnedhandModel;
  // Start Is Called Before The First Frame Update
  Void Start ()
  {
    List & lt; InputDevice & GT; Devices = New List & LT; InputDevice & GT; ();
    // INPUTDEVICECHARACTERSTICS RightControllercharacteristics = inputDevicecharacteristics.right | Inputdevicecharacteristics.controller;
    InputDevices.GetDevicesWithcharacteristics (Controllercharacteristics, Devices);
    Foreach (Var Item in Devices)
    {
      Debug.log (Item.Name + Item.characteristics);
    }
    if (devices.count & gt; 0)
    {
      targetdevice = devices [0];
      GameObject Prefab = ControllerPrefabs.find (Controller = & GT; Controller.name == targetDevice.Name);
      IF (Prefab)
      {
        Instantiate (Prefab, Transform);
      }
      ELSE.
      {
        Debug.logerror ("DID NOT FIND CORRESPONDING CONTROLLER MODEL");
        spawnedcontroller = instantiate (ControllerPrefabs [0], Transform);
      }
      spawnedhandmodel = instantiate (handmodelprefab, transform);
    }
  }
  // Update IS Called Once Per Frame
  Void Update ()
  {
    if (showcontroller)
    {
      spawnedhandmodel.setActive (False);
      spawnedcontroller.setactive (true);
    }
    ELSE.
    {
      spawnedcontroller.setactive (false); // 56 String
      spawnedhandmodel.setActive (TRUE);
    }
  }
 }

Answer 1, Authority 100%

I think so:

if (prefab)
  {
    spawnedcontroller = instantiate (prefab, transform); # fixed this string
  }
  ELSE.
  {
    Debug.logerror ("DID NOT FIND CORRESPONDING CONTROLLER MODEL");
    spawnedcontroller = instantiate (ControllerPrefabs [0], Transform);
  }

Otherwise, when executing the condition IF (Prefab) you have spawnedcontroller it remains not initialized and then closes the error more about it.
I don’t remember the Unity 3D, although I studied, but logically comes out something like that.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions