Text Wont Disable when Not in Range

So I have this script that is supposed to enable/disable a text game object stating “Press E To Interact.” Thanks to the Debug.Logs that I wrote, I noticed that “E disactive” (Yes English is my first language but I’m also sleep deprived why is why I didn’t write deactivated lol) always fires, but when in range of NPCS, “E active” fires followed immediately by “E disactive”

I’m unsure why, in range of NPCS, it’s still being registered that I’m in range then immediately out of range and going back and forth. If anyone can help, I’d appreciate it :]

public class PlayerInteract : MonoBehaviour {

	public float npcRange = 2.75f;
    public float itemRange = 1.75f;
    public GameObject E2Interact;

    //draw gizmos
    void OnDrawGizmosSelected() {
		Gizmos.color = Color.yellow;
		Gizmos.DrawWireSphere (transform.position, npcRange);
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, itemRange);
    }

	void Update () {
		Collider[] npcArray = Physics.OverlapSphere (transform.position, npcRange);
        Collider[] itemArray = Physics.OverlapSphere(transform.position, itemRange);
        
        //INTERACTING WITH NPCS
        foreach (Collider collider in npcArray) {
			NPCInteractable npci = collider.GetComponent<NPCInteractable> ();
            Dialogue dialogue = collider.GetComponent<Dialogue>();

            //if (npci == null && dialogue == null) {
                //E2Interact.SetActive(false);
            //}

			//now by pressing buttons
			if (Input.GetButtonDown ("Interact")) {
				if(npci != null) {
					npci.Interact();
                    E2Interact.SetActive(false);
                }
                if (dialogue != null)
                {
                    if (!dialogue.enabled)
                    {
                        E2Interact.SetActive(false);
                        dialogue.enabled = true;
                        dialogue.StartDialogue();
                    }
                }
            }
            //stuff to happen without pressing any buttons
            if (npci != null || dialogue != null)
            {
                E2Interact.SetActive(true);
                Debug.Log("E active");
            }
            else
            {
                E2Interact.SetActive(false);
                Debug.Log("E disactive");
            }
            if (npci != null)
            {
                npci.LookAt(this.transform);
            }
        }
        foreach (Collider collider in itemArray)
        {
            ItemPickup item = collider.GetComponent<ItemPickup>();
            if (item != null)
            {
                //SetFocus(interactable);
                Debug.Log("iNTERACTABLE");
                item.Interact();
            }
        }
    }
}

To fix this, you need to only disable E2Interact after checking all colliders and ensuring that none of them meet the criteria.

1 Like

void Update() {
Collider npcArray = Physics.OverlapSphere(transform.position, npcRange);
Collider itemArray = Physics.OverlapSphere(transform.position, itemRange);

bool isInNpcRange = false;

// INTERACTING WITH NPCS
for each (Collider collider in npcArray) {
    NPCInteractable npci = collider.GetComponent<NPCInteractable>();
    Dialogue dialogue = collider.GetComponent<Dialogue>();

    if (npci != null || dialogue != null) {
        isInNpcRange = true;
        Debug.Log("E active");

        if (Input.GetButtonDown("Interact")) {
            if (npci != null) {
                npci.Interact();
            }
            if (dialogue != null) {
                if (!dialogue.enabled) {
                    dialogue.enabled = true;
                    dialogue.StartDialogue();
                }
            }
        }

        if (npci != null) {
            npci.LookAt(this.transform);
        }
    }
}

// Toggle "Press E To Interact" based on NPC proximity
E2Interact.SetActive(isInNpcRange);

if (!isInNpcRange) {
    Debug.Log("E disactive");
}

// INTERACTING WITH ITEMS
for each (Collider collider in itemArray) {
    ItemPickup item = collider.GetComponent<ItemPickup>();
    if (item != null) {
        Debug.Log("Interactable Item in Range");
        item.Interact();
    }
}

}
Try This code, comment on your code, and used this code.

1 Like

This was some good stuff! But the problem seems to come back again. When I load up the scene, it says I’m out of range, which is a good start. When I’m in range, the text object activates and I get the log stating “E active.”

The issue is when I get out of range… nothing. Somehow nothing detects that I’ve left the NPC Interactable range, nothing states that npci is null, and the log “E disactive” isn’t firing at all. I’m really stumped about this one.

I tried adding the if statement

//getting out of range
if(npci == null && dialogue == null)
{
    isInNpcRange = false;
    Debug.Log("E disactive");
}

But it doesn’t help?? When I add this layer of protection to make sure it’s registered that I leave the range of NPCs, it goes back to where we started before:

  1. me being out of range gets the “E disactive” log firing
  2. me being in interactable range gets both logs firing
    Upon closer inspection, being in the range of an NPC gets 1 “E active” and 3 “E disactive” logs, so somehow every Update() is noticing that I’m in range once, and out of range three times? Even though I’m still in range.

Try above code and run the program .

Yeah that’s what I did. I commented out my original stuff and tried your code and it didn’t work as intended.