Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help Needed to solve a NullReferenceException

Discussion in 'Scripting' started by MINECRAFT0709, Nov 22, 2016.

  1. MINECRAFT0709

    MINECRAFT0709

    Joined:
    Nov 22, 2016
    Posts:
    2
    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 <Rigidbody>().velocity = gObj.GetComponent<Rigidbody>().velocity;
    h1.GetComponent<Rigidbody>().AddTorque(deltaPosition*1000);
    h1.GetComponent<Rigidbody>().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<Rigidbody>().velocity = gObj.GetComponent<Rigidbody>().velocity;
    h2.GetComponent<Rigidbody>().AddTorque(deltaPosition*1000);
    h2.GetComponent<Rigidbody>().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<Rigidbody>().velocity = gObj.GetComponent<Rigidbody>().velocity;
    GameObject h4 = (GameObject) Instantiate (quartApplePrefab, gObj.transform.position, gObj.transform.rotation);
    h3.GetComponent<Rigidbody>().AddTorque(deltaPosition*1000);
    h3.GetComponent<Rigidbody>().AddForce (deltaPosition*500);
    h4.transform.rotation *= Quaternion.Euler (0, -180, 90);
    h4.GetComponent<Rigidbody>().velocity =gObj.GetComponent<Rigidbody>().velocity;
    h4.transform.position += Vector3.up*7;
    h4.GetComponent<Rigidbody>().AddTorque(deltaPosition*1000);
    h4.GetComponent<Rigidbody>().AddForce (deltaPosition*500);
    Destroy (gObj);

    }

    }
    }

    }
    }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
  3. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    I doubt your code would even compile

    Code (CSharp):
    1. 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
     
    karl_jones likes this.
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    It's not invalid code, as it just results in doing nothing, but most static checkers would probably give you a warning.
     
  5. MINECRAFT0709

    MINECRAFT0709

    Joined:
    Nov 22, 2016
    Posts:
    2
    you were right vothka. Rookie mistake. Thanks for your help!