(c#) Object reference not set to an instance of an object
I have some code from the "CN controls" kit on the asset store and I have an error that pops up whenever I click on the screen.
Error is NullReferenceException: Object reference not set to an instance of an object CnControls.Dpad.OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/CNControls/Scripts/Controllers/Dpad.cs:31) CnControls.DpadInputHelper.Update () (at Assets/CNControls/Scripts/Controllers/InputHelpers/DpadInputHelper.cs:49)
code "DPad"
using UnityEngine;
using UnityEngine.EventSystems;
namespace CnControls
{
public class Dpad : MonoBehaviour
#if !UNITY_EDITOR
, IPointerDownHandler, IPointerUpHandler
#endif
{
public DpadAxis[] DpadAxis;
/// <summary>
/// Current event camera reference. Needed for the sake of Unity Remote input
/// </summary>
public Camera CurrentEventCamera { get; set; }
private void Awake()
{
#if UNITY_EDITOR
gameObject.AddComponent<DpadInputHelper>();
#endif
}
public void OnPointerDown(PointerEventData eventData)
{
CurrentEventCamera = eventData.pressEventCamera ?? CurrentEventCamera;
foreach (var dpadAxis in DpadAxis)
{
if (RectTransformUtility.RectangleContainsScreenPoint(dpadAxis.RectTransform, eventData.position,
CurrentEventCamera))
{
dpadAxis.Press(eventData.position, CurrentEventCamera, eventData.pointerId);
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
foreach (var dpadAxis in DpadAxis)
{
dpadAxis.TryRelease(eventData.pointerId);
}
}
}
}
DPadInputHelper
using UnityEngine;
namespace CnControls
{
/// <summary>
/// Helper class that contains additional logic needed for the In-Editor multitouch (Unity Remote)
/// Can be ommited when the Unity Remote system multitouch will be fixed
/// </summary>
public class DpadInputHelper : BaseInputHelper
{
private Dpad _linkedDpad;
protected override void Awake()
{
base.Awake();
_linkedDpad = GetComponent<Dpad>();
_linkedDpad.CurrentEventCamera = UiRootCamera;
}
private void Update()
{
// For every touch out there
for (int i = 0; i < CnInputManager.TouchCount; i++)
{
var touch = CnInputManager.GetTouch(i);
PointerEventDataCache.position = touch.position;
PointerEventDataCache.pointerId = touch.fingerId;
if (touch.phase == TouchPhase.Began)
{
_linkedDpad.OnPointerDown(PointerEventDataCache);
return;
}
if (touch.phase == TouchPhase.Ended)
{
_linkedDpad.OnPointerUp(PointerEventDataCache);
return;
}
}
// Mouse input here
// Same logic, but mouse is considered to be the finger with id of 255 so it's definitely won't interfere with actual fingers
PointerEventDataCache.position = Input.mousePosition;
PointerEventDataCache.pointerId = 255;
if (Input.GetMouseButtonDown(0))
{
_linkedDpad.OnPointerDown(PointerEventDataCache);
return;
}
if (Input.GetMouseButtonUp(0))
{
_linkedDpad.OnPointerUp(PointerEventDataCache);
return;
}
}
}
}
Thanks a ton for your time!
Answer by Raresh · Mar 15, 2016 at 03:33 PM
Line #17 script 2, when you assign '_linkedDpad = GetComponent();', make sure the game object that you attached the script to has the Dpad script on it. The error is because _linkedDpad is NULL when trying to access it's method.
Your answer
![](https://koobas.hobune.stream/wayback/20220612071932im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with Super Simple Jump Script 0 Answers
Changing (swapping) users movement controls IN GAME on trigger (2D) 0 Answers
Checking input from input.manager axes? 1 Answer
Canvas to appear on Airtap Gesture using IInputClickHandler 0 Answers
Is there anyhow to know if the axe is positive or negation 0 Answers