Problem with multiple instances of script displaying same text object

Hello I have 3 objects that have attached same script, this script calls and UI text when player triggers the collider, but this only works with one of the three objects, anyone knows why?

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

public class abrirPuerta : MonoBehaviour
{

    public Text abrirPuertaText;
    public Animator puerta;
    private bool show;
    public AudioClip pulsarBotonAudio;
    public AudioClip abrirPuertaAudio;
    private AudioSource audioSetup;
    
    void Start()
    {
        abrirPuertaText.text = "";
        show = false;
        puerta.SetBool("on", false);
        audioSetup = GetComponent<AudioSource>();
    }

    private void Update()
    {
        if (show)
        {
            abrirPuertaText.text = "Pulsa 'E' para abrir la puerta.";
        }
        else
        {
            abrirPuertaText.text = "";
        }

        if (Input.GetKeyDown(KeyCode.E) && show == true && puerta.GetBool("on") == false)
        {
            audioSetup.PlayOneShot(pulsarBotonAudio);
            audioSetup.PlayOneShot(abrirPuertaAudio);
            puerta.SetBool("on", true);
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.E) && show == true && puerta.GetBool("on") == true)
            {
                audioSetup.PlayOneShot(pulsarBotonAudio);
                audioSetup.PlayOneShot(abrirPuertaAudio);
                puerta.SetBool("on", false);
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            show = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            show = false;
        }
    }
}

Well, I understand you have that script on three objects, and the three reference the same UI Text object.

Within Update, the script assigns a new value to the Text. So, on every frame, the three will display either “Pulsa ‘E’ para abrir la puerta.” or nothing.

But they all do. So, the last one prevails.

Here’s a quick fix.

     private void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player"))
         {
              abrirPuertaText.text = "Pulsa 'E' para abrir la puerta.";
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.CompareTag("Player"))
         {
              abrirPuertaText.text = "";
         }
     }

This will work for a prototype, though I would consider a more solid architecture.

Honestly, I think there may be more we would need to know about the setup of the GOs (GameObjects), because the script look’s fine (although I’ve personally never seen anyone use CompareTag, because I (and many others) just use if (other.tag == "MyTag"), but each their own I guess). You even said, that one instance of the script works… so I am unsure if it’s the script’s fault.
Here are some general questions:

  • -Have you checked if the GOs, that won’t display the text, are even
    activated?
  • -Have you checked if the Script-instance is even activated?
  • -Have you tried logging Debug-messages for each step in the
    script?
  • -Have you checked whether or not the Collider-volumes are set correctly
    and activated?
  • -Have you tried attaching that script to a different GO and using it’s
    Collider as Trigger, and testing the
    script that way?
  • -Have you checked the Tag-relationship (i.e. how the Tags
    in your project interact. Maybe
    there’s an arbitrary error somewhere.
    But by default there shouldn’t be any
    though.)?

I hope that help’s you just a bit, but feel free to contact me directly, or here using the comment-section, if you’re still experiencing errors.