• 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 Haarsuppe89 · Nov 02, 2020 at 10:03 AM · augmented-realitydebuggingdrag-and-drop

ARCore: Nullpointer exception after touchphase ended. Drag and Drop problem.

I made a script in ARCore to drag and drop three objects: A shield, a cube and a dice. Each gameobject has a Outline shader script to mark the object with outline color after touching it. The script is almost working, but at the beginning when the app started I always receive an error nullpointer exception after lifting my finger from the screen. After I tapped on one of the three gameobjects the exception is gone. In my code I added a condition to check if the gameobject is set with a certain component or not. I want to disable the rigidbody of the dice, because it is complicated to drag an object which has a rigidbody.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Timers;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.XR.ARFoundation;
 
 public class Test : MonoBehaviour
 {
     private int tapCount = 0;
     private GameObject placement;
     public Camera arCamera;
     private float doubleTapTimer = 0;
     private Color oldColor;
 
     private float range;
     private bool movingVertical = false;
     private bool moving = false;
 
     private void Start()
     {
         range = 1000f;
         oldColor = new Color(0.96f, 0.46f, 0.078f);
     }
 
     void Update()
     {
         doubleTapTimer += Time.deltaTime;
         
         for (int i = 0; i < Input.touchCount; i++)
         {
             Ray ray = arCamera.ScreenPointToRay(Input.GetTouch(i).position);
             RaycastHit hitObject;
             
             // One-Finger Touch one time
             if (i == 0)
             {
                 if (Input.GetTouch(i).phase == TouchPhase.Began)
                 {
                     tapCount += 1;
                 }
 
                 if (tapCount == 0 && !moving)
                 {
                     if (Physics.Raycast(ray, out hitObject, range))
                     {
                         if (hitObject.transform.name != null)
                             Debug.Log(hitObject.transform.name);
                         
                         switch (hitObject.transform.name)
                         {
                             case "Cube":
                             case "WhiteDice":
                             case "Shield":
                                 ActivateObject(hitObject);
                                 moving = true;
 
                                 break;
                             default:
                                 ResetObject(placement);
                                 break;
                         }
                     }
                 }
 
                 // One-Finger Touch two times
 
                 if (tapCount == 1 && doubleTapTimer < 0.5f && !(moving))
                 {
 
                     if (Physics.Raycast(ray, out hitObject, range))
                     {
 
                         switch (hitObject.transform.name)
                         {
                             case "Cube":
                             case "WhiteDice":
                             case "Shield":
                                 movingVertical = true;
                                 placement.GetComponent<Outline>().OutlineColor = Color.blue;
                                 break;
                             default:
                                 break;
                         }
                     }
 
                 }
 
 
                 if (moving)
                 {
                     if (Physics.Raycast(ray, out hitObject, range))
                     {
                         if (placement.name.Contains("WhiteDice") && 
                             placement.GetComponent<DiceScript>() != null &&
                             placement.GetComponent<Rigidbody>() != null)
                         {
                             placement.GetComponent<DiceScript>().enabled = false;
                             placement.GetComponent<Rigidbody>().isKinematic = true;
                         }
                         
                         if (!movingVertical)
                         {
                             placement.transform.position = new Vector3(hitObject.point.x,
                                 placement.transform.position.y, hitObject.point.z);
                         }
                         else if (movingVertical)
                         {
                             placement.GetComponent<Outline>().OutlineColor = Color.blue;
                             placement.transform.position = new Vector3(placement.transform.position.x,
                                 hitObject.point.y, placement.transform.position.z);
                         }
 
                     }
                 }
 
                 if (doubleTapTimer > 0.5f)
                 {
                     doubleTapTimer = 0f;
                     tapCount = 0;
                 }
             }
 
             // Double Finger Touch one times
             if (Input.GetTouch(i).phase == TouchPhase.Ended)
             {
                 moving = false;
                 movingVertical = false;
                 if (placement != null)
                 {
                     placement.GetComponent<Outline>().enabled = false;
                 }
 
                 if (movingVertical)
                 {
                     tapCount = 0;
                     doubleTapTimer = 0.0f;
                 }
                 
                 if (placement.name.Contains("WhiteDice") && 
                     placement.GetComponent<DiceScript>() != null &&
                     placement.GetComponent<Rigidbody>() != null)
                 {
                     placement.GetComponent<DiceScript>().enabled = true;
                     placement.GetComponent<Rigidbody>().isKinematic = false;
                 }
             }
         }
     }
 
     public void ActivateObject(RaycastHit hitObject)
     {
         ResetObject(placement);
         doubleTapTimer = 0f;
         placement = hitObject.transform.gameObject;
         placement.GetComponent<Outline>().enabled = true;
         placement.GetComponent<Outline>().OutlineColor = oldColor;
     }
     
     public void ResetObject(GameObject placement)
     {
         if (placement != null)
         {
             placement.GetComponent<Outline>().OutlineColor = oldColor;
             placement.GetComponent<Outline>().enabled = false;
             placement = null;
             moving = false;
             tapCount = 0;
             doubleTapTimer = 0f;
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

How do you execute code every X angles reliably? 2 Answers

Visual Studio 2017 Diagnostic Tools "The Diagnostic Tools window does not support the current debugging configuration" 0 Answers

I renamed the Unity Project and my Game Stopped Working 1 Answer

Camera display when using it to build and run in android 0 Answers

Clip 3D Object to specific volume 2 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