• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Set 3D animation as Legacy(Debug Menu Grayed Out!) 0 Answers

Modify 'Copy PDB Files' via code ? 1 Answer

MySQL Connector : Runtime Error issue 0 Answers

Trouble Attaching Unity Editor to Process for MonoDevelop debugging'... 1 Answer

Interaction of objects owned different Vuforia image targets. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges