Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TimeCastle · Mar 15, 2016 at 03:21 PM · c#inputmanager

(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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimeCastle · Mar 15, 2016 at 06:05 PM 0
Share

Thanks for the help I fixed it now

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges