Jump Button needs to do two things

I’m trying to develop my game with consoles in mind, so buttons often serve multiple purposes, either to keep things simple for the player or due to input limitations on standard controllers.
With that in mind I want to use one button to Jump and Talk. The talking side works fine;

if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
        {
            if (hit.collider.tag.Equals("NPC"))
            {
                if (talking == true && DiaCount == 0){
                    hit.collider.GetComponent<DialogueTrigger>().Invoke("TriggerDialogue", 0f);

                } else if (DiaCount > 0) {
                    pin.GetComponent<DialogueManager>().Invoke("DisplayNextSentence", 0f);
                }
            }
            else if (hit.collider.tag.Equals(null))
            {
                rb.velocity = Vector3.up * jumpForce;
            }

When I invoke the speech version of this (with the text boxes and whatnot) it works pretty much like a charm. When I try to invoke the Jump however, it comes up with “object set to null reference” as an error. The idea is that I don’t want the player to jump when talking with an NPC, but I do at pretty much any other time so long as they’re on the ground.

Anybody got any ideas on what I can do with this? I tried taking out the if (hit.collider.tag.Equals(null)) to see if that made any difference but it comes back with the same error.

I appreciate any assistance!

Hi there!

I’m not quite sure exactly what is causing the error. I’m guessing nothing was actually “hit”, so you are trying to grab the collider and tag of null? But you said you removed that line and it still happened, so I dunno. I’d have to see more code to know exactly what you’ve got the error.


However, give something like this a shot. Whenever you press space, create a circle and check if there are interactable things nearby. If so, interact obviously, if not, jump. The code here is written for 2D, but if you’re wanting 3D, I’m pretty sure the same would work, you’d just need to change a couple things. Let me know if this helps!

if (Input.GetKeyDown(KeyCode.Space)) {

    bool canInteract = false;
    Collider2D interactableCollider = null;
    Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, interactRadius);

    // Checks if there is something that can be interacted with nearby
    foreach (Collider2D col in colliders) {
        if (col.tag.Equals("NPC")) {
            canInteract = true;
            interactableCollider = col;
        }
    }

    if (canInteract) {
        if (talking == true && DiaCount == 0) {
            interactableCollider.GetComponent<DialogueTrigger>().Invoke("TriggerDialogue", 0f);
        }
        else if (DiaCount > 0) {
            // I don't know what pin is but yeah
            pin.GetComponent<DialogueManager>().Invoke("DisplayNextSentence", 0f);
        }
    }
    else {
        rb.velocity = Vector3.up * jumpForce;
    }

}