K
K
KaRaMiD2021-04-22 12:35:53
Unity
KaRaMiD, 2021-04-22 12:35:53

How to do pickup in Unity 2D?

When I go to the box and press the English letter E, nothing happens, but
when I switch the Hold variable to true in Unity, I fly away somewhere.
And here's the error when I just stand there and change Hold to true:

NullReferenceException: Object reference not set to an instance of an object
BoxHold.Update () (at Assets/Scrips/BoxHold.cs:44)

Here's what's in Unity.
608142c1969c7145857908.png
Script code for picking up boxes:

public class BoxHold : MonoBehaviour
{

    public bool hold;
    public float distanse = 3f;
    RaycastHit2D hit;
    public Transform holdPoint;
    public float throwObject = 5f;

    void Start()
    {
        
    }
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (!hold)
            {
                Physics2D.queriesStartInColliders = false;
                hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distanse);

                if (hit.collider != null && hit.collider.tag == "box")
                {
                    hold = true;
                }
            }
            else
            {
                hold = false;
                if (hit.collider.gameObject.GetComponent<Rigidbody2D>() != null)
                {
                    hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1) * throwObject;
                }
            }
        }
        if (hold)
        {
            hit.collider.gameObject.transform.position = holdPoint.position;
        }
    }
    private void onDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawLine(transform.position, transform.position + Vector3.right * transform.localScale.x * distanse);
    }
}

Player script code:

public class PlayerControl : MonoBehaviour
{

    public float move = 5f;
    public GameObject obj;
    public float jumpForse;

    public bool isGrounded, isBox;
    public Transform groundCheck;
    public float groundRadius;
    public LayerMask whatIsGround, whatIsBox;

    private bool ifRight = true;
    private float moveInput;
    private Rigidbody2D rb;

    private int extraJump;
    public int extraJumpValue;

    private void Start()
    {
        extraJump = extraJumpValue;
        print("hello!");
        rb = GetComponent<Rigidbody2D>();
    }

    void Flip()
    {
        ifRight = !ifRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        isBox = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsBox);
        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * move, rb.velocity.y);
        if (ifRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (ifRight == true && moveInput < 0)
        {
            Flip();
        }
    }

    private void Update()
    {
        if (isGrounded == true || isBox == true)
        {
            extraJump = extraJumpValue;

        }
        if (Input.GetKeyDown(KeyCode.W) && extraJump > 0)
        {
            rb.velocity = Vector2.up * jumpForse;
            extraJump--;
        }
        else if (Input.GetKeyDown(KeyCode.W) && extraJump == 0 && isGrounded == true || Input.GetKeyDown(KeyCode.W) && extraJump == 0 && isBox == true)
        {
            rb.velocity = Vector2.up * jumpForse;
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kokapuk, 2021-04-22
@kokapuk

make the object a trigger and already in the OnTriggerEnter2D code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question