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. Dismiss Notice

Question ThrowScript doesnt work: Object reference not set to an instance of an object

Discussion in 'Scripting' started by RubenVanOostveen, Mar 4, 2021.

  1. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    im attempting to make an script where I can drag items but I get code:

    Inspector:
    upload_2021-3-4_21-29-17.png

    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PickUpThrow : MonoBehaviour
    6. {
    7.     public Transform ObjectHolder;
    8.     public float ThrowForce;
    9.     public bool carryObject;
    10.     public GameObject Item;
    11.     public bool IsThrowable;
    12.  
    13.     private void Start()
    14.     {
    15.      
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.E))
    22.         {
    23.             RaycastHit hit;
    24.             Ray directionRay = new Ray(transform.position, transform.forward);
    25.             if (Physics.Raycast(directionRay, out hit, 2f))
    26.             {
    27.                 if (hit.collider.tag == "Object")
    28.                 {
    29.                     carryObject = true;
    30.                     IsThrowable = true;
    31.                     if (carryObject == true)
    32.                     {
    33.                         Item = hit.collider.gameObject;
    34.                         Item.transform.SetParent(ObjectHolder);
    35.                         Item.gameObject.transform.position = ObjectHolder.position;
    36.                         Item.GetComponent<Rigidbody>().isKinematic = true;
    37.                         Item.GetComponent<Rigidbody>().useGravity = false;
    38.                     }
    39.                 }
    40.             }
    41.         }
    42.         if (Input.GetMouseButton(1))
    43.         {
    44.             carryObject = false;
    45.             IsThrowable = false;
    46.         }
    47.         if (carryObject == false)
    48.         {
    49.             ObjectHolder.DetachChildren();
    50.             Item.GetComponent<Rigidbody>().isKinematic = false;
    51.             Item.GetComponent<Rigidbody>().useGravity = true;
    52.         }
    53.         if (Input.GetMouseButton(0))
    54.         {
    55.             if (IsThrowable)
    56.             {
    57.                 ObjectHolder.DetachChildren();
    58.                 Item.GetComponent<Rigidbody>().isKinematic = false;
    59.                 Item.GetComponent<Rigidbody>().useGravity = true;
    60.                 Item.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * ThrowForce);
    61.             }
    62.         }
    63.     }
    64.  
    65. }
    Tutorial i followed:
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  3. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    I did but the dude in the tutorial didn't do anything with the ITEM Exception. ANd he didn't got an error. However I do