Help Needed to solve a NullReferenceException

I am making a simple fruit ninja game. I got the below error and I am not able to fix it. Any help/direction would be great.

NullReferenceException: Object reference not set to an instance of an object
SwipeTrail.Update () (at Assets/Scripts/SwipeTrail.cs:72)

This is the script (C#).

using UnityEngine;
using System.Collections;

public class SwipeTrail : MonoBehaviour {

//video 3 - Mobile Dev from Scratch: Float Up Text and Scores

public GameObject applePrefab;
public GameObject halfApplePrefab;
public GameObject quartApplePrefab;
GameObject gObj = null;
bool swipeCoolDown = true;

Vector3 lastPosition;
Vector3 deltaPosition;

Ray GenerateMouseRay (Vector3 touchPos)
{
Vector3 mousePosFar = new Vector3 (touchPos.x, touchPos.y, Camera.main.farClipPlane);
Vector3 mousePosNear = new Vector3 (touchPos.x, touchPos.y, Camera.main.nearClipPlane);

Vector3 mousePosF = Camera.main.ScreenToWorldPoint (mousePosFar);
Vector3 mousePosN = Camera.main.ScreenToWorldPoint (mousePosNear);

Ray mr = new Ray (mousePosN, mousePosF - mousePosN);
return mr;
}

// Use this for initialization

void CoolDown ()
{
swipeCoolDown = true;

}

void Update ()
{

gObj = GameObject.Find (“SingleApple”);

if (((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) || Input.GetMouseButtonDown (0))) {
Plane objPlane = new Plane (Camera.main.transform.forward * -1, this.transform.position);
Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
float rayDistance;
if (objPlane.Raycast (mRay, out rayDistance))
lastPosition = mRay.GetPoint (rayDistance);

}

else if (((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) || Input.GetMouseButton (0))) {

Plane objPlane = new Plane (Camera.main.transform.forward * -1, this.transform.position);
Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
float rayDistance;
if (objPlane.Raycast (mRay, out rayDistance))
this.transform.position = mRay.GetPoint (rayDistance);

Ray mouseRay = GenerateMouseRay (Input.mousePosition);
RaycastHit hit;
deltaPosition = this.transform.position-lastPosition;
lastPosition = this.transform.position;

if (Physics.Raycast (mouseRay.origin, mouseRay.direction, out hit)&&swipeCoolDown);
{
//Line 72 below
gObj= hit.transform.gameObject;
swipeCoolDown=false;
Invoke (“CoolDown”,0.5f);

if(gObj.tag==“whole”)
{
GameObject h1 = (GameObject) Instantiate (halfApplePrefab, gObj.transform.position, gObj.transform.rotation);
h1.transform.rotation = Quaternion.Euler (-90, -90, 0);
hit.transform.Translate (0,0, -5);
h1.GetComponent ().velocity = gObj.GetComponent().velocity;
h1.GetComponent().AddTorque(deltaPosition
1000);
h1.GetComponent().AddForce (deltaPosition*500);

GameObject h2 = (GameObject) Instantiate (halfApplePrefab, gObj.transform.position, gObj.transform.rotation);
h2.transform.rotation = Quaternion.Euler (90, 0, 120);
// hit.transform.Translate (0,0,-5);
h2.transform.position += Vector3.up
7;
h2.GetComponent().velocity = gObj.GetComponent().velocity;
h2.GetComponent().AddTorque(deltaPosition1000);
h2.GetComponent().AddForce (deltaPosition
500);
Destroy (gObj);

}

else if(gObj.tag==“half”) {

GameObject h3 = (GameObject) Instantiate (quartApplePrefab, gObj.transform.position, gObj.transform.rotation);
h3.transform.rotation = Quaternion.Euler (-90, 0, -90);
h3.GetComponent().velocity = gObj.GetComponent().velocity;
GameObject h4 = (GameObject) Instantiate (quartApplePrefab, gObj.transform.position, gObj.transform.rotation);
h3.GetComponent().AddTorque(deltaPosition
1000);
h3.GetComponent().AddForce (deltaPosition500);
h4.transform.rotation = Quaternion.Euler (0, -180, 90);
h4.GetComponent().velocity =gObj.GetComponent().velocity;
h4.transform.position += Vector3.up
7;
h4.GetComponent().AddTorque(deltaPosition
1000);
h4.GetComponent().AddForce (deltaPosition*500);
Destroy (gObj);

}

}
}

}
}

Please try to use code tags when posting code, it makes it much easier to read.

Take a look at this: Unity - Manual: Null Reference Exceptions

I doubt your code would even compile

if (Physics.Raycast (mouseRay.origin, mouseRay.direction, out hit)&&swipeCoolDown);

you are ending your line with a “;” which should not be there, therefore i would think your “hit” is null

1 Like

It’s not invalid code, as it just results in doing nothing, but most static checkers would probably give you a warning.

you were right vothka. Rookie mistake. Thanks for your help!