- Home /
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;
}
}
}
Answer by UnityCoach · Jul 22, 2017 at 04:31 PM
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.
Yeah, that should get rid of that unwanted behaviour. +1
what should be more solid architecture? just for learning purposes, it will be great to learn a bit more about the correct method, anyways ur method seems to work fine
Answer by Lili-Shi · Jul 22, 2017 at 01:08 PM
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.
Seems like other functions of the script are working like display a sound and play an animation on all prefabs but the text wont show on all, just in the first one i set on the scene, dunno what's wrong
Well, then I would do what UnityCoach wrote down. It'll work for now, although you should come back to that issue and write more solid code, as he also say's.