how to make a text object disappear while out of trigger zone?

hi guys, new guy here.
for the past few days i’ve been working on creating an “e” letter that pops up whenever the player is next to an interact-able NPC. the problem i’m having now is that i want that letter to disappear when the player leaves the trigger zone of the interaction, and re appear if he comes back.
this means that the text pop up code is inside an if statement and is supposed to execute if(triggering) but i don’t know how to tell it to make the text disappear if not triggering.

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

public class WomanNPC : MonoBehaviour
{
    public GameObject FloatingTextPrefab;
    private GameObject triggeringPlayer;
    private bool triggering;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (triggering)
        {
            if (FloatingTextPrefab)
            {
                ShowFloatingText();
            }
        }
       
       
    }



    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            triggering = true;
            triggeringPlayer = other.gameObject;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            triggering = false;
            triggeringPlayer = null;
        }
    }
    void ShowFloatingText()
    {
        Instantiate(FloatingTextPrefab, transform.position, Quaternion.identity, transform);
    }
}

thanks!

You’re close! All you need to do is say the opposite: if not triggering, hide floating text. You can destroy it if you like, since you are on-demand instantiating it.

HOWEVER: Your code above appears to be instantiating a new text every frame… you’re calling it as long as you have a prefab and triggering is true. That’s gonna make like 60 texts per second, which is unlikely to be what you want.

INSTEAD, do away with Update() entirely and just call Show and Hide floating text from within the triggers.

You might still need to set and clear the triggering boolean so someone else can know if they can interact, but maybe you’re handling that somehow else.

1 Like

kurt i gotta see you’re helping me out pretty consistently lately, so thanks a lot! since i’m working off of tutorials im very limited to one method (in a literal sense, not methods in code) so i can’t really veer away from what the guy in the video tells me.
anyway, back to your first suggestion, i’ve not manged to get it to work. this is how i tried to formulate it:

 if(!triggering)
        {

            HideFloatingText();
        }
      

//or:

else
        {

            HideFloatingText();
        }

both the if/!triggering and the else/hide don’t work. because “HideFloatingText does not exist in the current context”

also, destroyinh it is fine but afterwards the object doesn’t comeback

I would slightly revise your approach to tutorials so that one of the side-tasks you give yourself is “What would happen if I did X to this thing?”

If it breaks or doesn’t do anything, revert it back the way it was and continue.

I mean how else are you gonna learn?? Learning is the modification of behavior as a result of experience, so if you don’t experience branching away a little bit, all you are learning is how to do tutorials, not how to make games. :slight_smile:

1 Like

You see the function ShowFloatingText()? See how it Instantiates something? That’s why I suggested Destroying() it in the HideFloatingText()… but that is a new function you have to write yourself. But if you prefer to stay close to how the tutorial is doing stuff, er, good luck I guess. Maybe ask the tutorial guy? Maybe he’ll fix the problem you see in the next video?? That’s kinda why I suggest starting to consider what all this funny text you’re typing in is doing and speculate on interesting ways to change it meaningfully. :slight_smile:

Just to add one more thing, when you call Instantiate, it returns a reference to what it created. You would need to store that in a member variable so you know what you have to destroy later.

The usual Unity pattern flow is:

prefab ← assigned in inspector - this is on disk and used as a template

you Instantiate the prefab and assign the return value to a local variable (call it “myThingy”)

you Destroy() the copy you made, usually by calling Destroy( myThingy) when you’re done.

1 Like

that’s usually how i do toturials actually, i always try to re-phrase everything i do, so i agree 100% with what you said. the promleb is i don’t know how to rephrase stuff, because i’m limited with my knowledge in code to what i’ve learn so far (which is not a lot), however back to the subject at hand:
if i understand you correctly, you say that i should use the Instantiates to create a member so that the Destroy() won’t destroy the reference but the copy? it makes since to me since whenever the text got destroyed it never came back. i guess that’s cause i destroyed the whole object and not just it’s copy.

as for the toturial, well… it was made for creating floating “damage” text that indicates how much health the enemy has, i’m trying to use it for something different as you see, so the toturial guy isn’t really there for me.

could you btw recommend more tutorials and videos?

Please help? how do i create a destroy method?