• 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 /
avatar image
-1
Question by NinjaRubberBand · Dec 06, 2016 at 04:42 PM · 2dscripting problemraycast

Object reference not set to an instance of an object

I know this question is always asked, but in my case i haven't found a solution. I have this script: using UnityEngine; using System.Collections; using System.Collections.Generic;

  public class TouchInput : MonoBehaviour {
  
      public LayerMask touchInputMask;
      
      private List<GameObject> touchList = new List<GameObject>();
      private GameObject[] touchesOld;
      public RaycastHit2D hit;
     public GameObject recipient;
      
      // Update is called once per frame
      void Update () {
          
          
  #if UNITY_EDITOR
      
      if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0)  || Input.GetMouseButtonUp(0)) {
          
          touchesOld = new GameObject[touchList.Count];
              touchList.CopyTo(touchesOld);
              touchList.Clear();
   
          
          
                    //Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                  
                    
                     if(Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero)) {
                        
                       recipient = hit.transform.gameObject;
                        touchList.Add(recipient);
                        
                        if(Input.GetMouseButtonDown(0)) {
                            recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                            
                        }  if(Input.GetMouseButtonDown(0)) {
                            recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                            
                        }  if(Input.GetMouseButtonUp(0)) {
                            recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                            
                        }  
                  
      
                      }
                   
                   foreach (GameObject g in touchesOld) {
                       if(!touchList.Contains(g)) {
                           g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                       }
                       
                   }
          
              }
      
  #endif
          
          
          
          
          
      
          }
  }


Its a script for detection touch inputs, for when u play the game on touch devices. This script is attached to the main camera, and when i click a 2d object with the layermask "touchinputmash", and a script saying something like: void OnTouchDown () { print("NICE!"); }

But when i click my desired 2d object i get the warning: NullReferenceException: Object reference not set to an instance of an object TouchInput.Update () (at Assets/Scripts/TouchInput.cs:33).

So its something with the line: recipient = hit.transform.gameObject;

But i cant figure out what.

Comment
Add comment · Show 5
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 edcx · Dec 06, 2016 at 05:01 PM 0
Share

I don't see where you assign the value to hit. So I assume that it is null. It really helps if you learn how to use a debugger or you can use Debug.Log() to check suspicious variables like Debug.Log("Is hit null: " + (hit == null)) Debug.Log("Is recipient null: " + (recipient == null))

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Paricus · Dec 06, 2016 at 05:34 PM

it looks like you're missing out hit in your raycast

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 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 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

Confused about ITilemap interface and concrete tilemap classes 0 Answers

Layer Mask on raycast2d not working? 1 Answer

Finding any objects below or above object at ANY POINT? 1 Answer

Getting a Raycast rope to follow its object 0 Answers

2D Character Controller Movement Issues 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