- Home /
I want to make text appear for a few seconds and then disappear.
The text is when player don't have the item. It's like "you don't have this item...". It's on line 39 & 40. Basically i want to make it from true to false for noKeyUI and false to true for interactUI. This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerInteraction : MonoBehaviour
{
[SerializeField] private Text interactUI;
[SerializeField] private Text noKeyUI;
public GameObject currentObj = null;
public ObjectInteraction currentObjScript = null;
public Inventory inventory;
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && currentObj)
{
if (currentObjScript.inventory)
{
inventory.AddItem(currentObj);
}
//check if the door can be opened
if (currentObjScript.canOpen)
{
//check if the door is locked
if (currentObjScript.locked)
{
//check if player have the key - unlock if have the key
if (inventory.FindItem(currentObjScript.itemNeed))
{
currentObjScript.locked = false;
}
else
{
noKeyUI.gameObject.SetActive(true);
interactUI.gameObject.SetActive(false);
}
}
else
{
currentObjScript.SwitchLevel();
}
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("InterObj"))
{
interactUI.gameObject.SetActive(true);
currentObj = other.gameObject;
currentObjScript = currentObj.GetComponent<ObjectInteraction>();
}
}
void OnTriggerExit2D(Collider2D other)
{
interactUI.gameObject.SetActive(false);
if (other.CompareTag("InterObj"))
{
if (other.gameObject == currentObj)
{
currentObj = null;
}
}
}
}
Answer by sokolllll89 · Feb 12, 2020 at 01:08 PM
May use Invoke();
I've tried to use Invoke, but it didn't work or maybe i did it wrong cause i don't really know how to use invoke. But i've try other method and it works. Btw, thanks for the reply.
Answer by edthered1009 · Feb 13, 2020 at 01:29 AM
I'd recommend using a coroutine. Coroutines are very useful for timers, and are quite easy to use once you know how. You can do:
IEnumerator CoroutineName() { yield return new WaitForSeconds(delay); noKeyUI.active = false; }
You can call it whenever you want by doing StartCoroutine(CoroutineName());
. Hope this helps!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[SOLVED] Why is text not appearing? 2 Answers
Converting TextMeshPro text to TextMeshPro UGUI 0 Answers
Planning Text Scroll with Unity UI 0 Answers