Text Will not disappear after Object Destroyed,Text Wont Go away after object disappears.
I have created a simple script that detects if a player is within range of an interactable object. If the player is within range, a text will appear telling the player to interact with the object.What is meant to occur is that after the player interacts with the object, the text will disappear, but this does not occur, it seems simple but i have been stuck on this for a while. Any help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class InteractableObjects : MonoBehaviour {
public GameObject Text;
public bool isInteractable = false;
public GameObject Object;
public virtual void Interaction()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
isInteractable = true;
Text.SetActive(true);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
Text.SetActive(false);
isInteractable =false;
}
}
}
Code 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUpItem : InteractableObjects {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (isInteractable && Input.GetKeyDown(KeyCode.F))
{
Interaction();
}
}
public override void Interaction()
{
base.Interaction();
Debug.Log("Item Picked up"); //Item disappears from game
Destroy(gameObject);
}
}
here is a quick demonstration of the issue. https://gyazo.com/a48a2998039f1a83dc96bf0ef725b555
Answer by Vega4Life · Dec 12, 2018 at 11:17 PM
You aren't destroying the text - only the InteractableObject. The easiest thing is to add it to the base interaction so it gets destroyed after the override is called through base.Interaction();
public virtual void Interaction()
{
Destroy(Text.gameObject);
}
Your answer
Follow this Question
Related Questions
Weird text field bug? 1 Answer
OnTriggerEnter For Parking 1 Answer
BCE0005: Unknown identifier: 'Cardboard'. 1 Answer