Not detecting object

Hello, we are making a game and there is a problem with the code(?) I am currently writing.

It’s a Rigitbody Half-Life style object pickup.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPickup : MonoBehaviour
{
    public bool isHoldingRigidbody = false;
    public Vector3 holdedRigidbodyScale;
    public float holdedRigidbodyMass = 0;
    public GameObject holdingRigidbodyObject;

    void Update()
    {
       PhysicPickUp();
    }

    public void PhysicPickUp()
    {
        if (Input.GetKeyDown(KeyCode.E) && !isHoldingRigidbody) // Corrected condition
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                holdingRigidbodyObject = hit.collider.gameObject;

                // Use GetComponent to get the Renderer component
                Renderer renderer = holdingRigidbodyObject.GetComponent<Renderer>();

                // Check if the Renderer component exists before accessing it
                if (renderer != null)
                {
                    if (Vector3.Distance(this.gameObject.transform.position, holdingRigidbodyObject.transform.position) < 3f &&
                                            renderer.isVisible &&
                                            holdingRigidbodyObject.GetComponent<Rigidbody>() != null)
                    {
                        holdedRigidbodyScale = holdingRigidbodyObject.transform.localScale;
                        holdingRigidbodyObject.transform.parent = this.gameObject.transform;
                        holdedRigidbodyMass = holdingRigidbodyObject.GetComponent<Rigidbody>().mass;
                        Destroy(holdingRigidbodyObject.GetComponent<Rigidbody>());
                        isHoldingRigidbody = true;
                    }
                }
            }
        }

        if (Input.GetKeyUp(KeyCode.E) && isHoldingRigidbody) // Corrected condition
        {
            PhysicThrow();
        }

        if (Input.GetMouseButton(0) && isHoldingRigidbody) // Corrected condition
        {
            PhysicThrow(20);
        }
    }

    private void PhysicThrow(float force = 0)
    {
        holdingRigidbodyObject.transform.parent = null;
        holdingRigidbodyObject.AddComponent<Rigidbody>();
        holdingRigidbodyObject.GetComponent<Rigidbody>().mass = holdedRigidbodyMass;
        holdingRigidbodyObject.transform.localScale = holdedRigidbodyScale;

        if (force != 0)
        {
            holdingRigidbodyObject.GetComponent<Rigidbody>().AddForce(this.gameObject.transform.forward * force, ForceMode.Impulse);
        }

        isHoldingRigidbody = false;
    }
}

I can’t find what the problem is.

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

If you are working from a tutorial, remember these two steps for tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Ok I think I should have posted this also
Screenshot 2024-08-27 215409

This what I get when launching (even if I set the object manually). It should grab he object when you press E.

You absolutely NEVER need to post a NullReferenceException! You need to go fix it.

The basic 3 steps are:

  1. Identify what is null ← any other action taken before this step is WASTED TIME
  2. Identify why it is null
  3. 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.

NullReference is the single most common error while programming. Fixing it is always the same.

  • 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

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:

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

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

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

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

The script looks okay. Although it does require that the user holds down the E key to carry the object around. And the object to be carried also needs to have a rigidbody component.

The thing is, I created object and set the rigibody component and when I start (and holding E is necessery, the half-life/VoTV/ any other game that have physics) in gives me the null.

Well the script is fine and so your scene setup is causing the problem. Perhaps you have something attached to your player that is blocking the raycast from the camera. A gun perhaps?. Insert a Debug.Log(hit.transform.name); at line 27 to see if the raycast is hitting the object you wish to carry and not some other object in the scene.

maybe it’s a player pill that is used as the body :thinking:

No I have moved the camera a little out of the body so the cam part i not touchint the body and it’s not working.

That’s three posts without doing Step #1 above.

Go figure out WHAT IS NULL. What variable, what field, what expression, what whatever.

Until you do that this won’t get fixed.

1 Like

If a raycast starts from within an object then the object will be ignored and so it’s okay to start a raycast from inside of the player’s capsule collider.

Insert a Debug.Log(hit.transform.name); at line 27 to see which object is being hit by the raycast when looking at the object you wish to carry.