Advertisement

(unity+C#) How do i create a method to hide a text object, once the player leaves the trigger zone?

Started by July 16, 2020 03:24 PM
1 comment, last by Rutin 4 years, 4 months ago

hello all. i need help for a simple problem, but might require long explanation. programm

i've created a text object that is the letter E that pops up whenever the player gets close to an NPC. it works, but the letter stays there forever.

at first i've created (based on some tutorial) a destroy command, but once it's destroyed it won't reappear, even though i've used a method with “Instantiate” and even though it's placed in the update method.

i simply would like to know what lines of code i should put where, and of course an explanation about them, to make the object disappear when outside the trigger zone (which is based on box colliders) and reappear when the player gets back.

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

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

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

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



    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);
    }
    void HideFloatingText()
    {
        Destroy(FloatingTextPrefab);
    }

}

this is the NPC's code, on which is based the floating text object for it's location purposes. right now the “else” method obviously's not working, as well as the “HideFloatingText” method i've tried to make.

thanks!

None

It has been awhile since I used Unity, but I believe you just need to do something like this to only disable rendering for an object:

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

public class ri : MonoBehaviour
{
    public Renderer r;

    // Start is called before the first frame update
    void Start()
    {
        r.GetComponent<Renderer>();

        r.enabled = false; // Disable Rendering
        // r.endbled = true; // Enable Rendering
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Disabling the rendering will not deactivate any physics for the object so keep that in mind. I don't think it matters in your case though.

To disable the Game Object use:

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

public class ri : MonoBehaviour
{
    public GameObject o;

    // Start is called before the first frame update
    void Start()
    {
        o.SetActive(false); // Disable GameObject
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

As per the above with the Renderer, just reference the object in the same way I showed above.

Incorporate that into your code and you'll be good to go.

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement