#Vault UI (Resolved)
1 messages · Page 1 of 1 (latest)
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class vaultTip : MonoBehaviour
{
public GameObject vaultUI;
private BoxCollider2D vault;
public void Start() {
vault = this.GetComponent<BoxCollider2D>();
vaultUI.SetActive(false);
}
public void Update() {
if (Input.GetKeyDown(KeyCode.Escape)) {
closeUI();
}
}
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Player") {
vaultUI.SetActive(true);
Time.timeScale = 0;
Debug.Log($"{vaultUI.name}'s active state was set to true", vaultUI);
}
}
public void closeUI() {
vaultUI.SetActive(false);
Time.timeScale = 1;
}
}
so whats happening is the first time I interact, it doesnt pop up, however, once I pressed escape to closeui, aand reenter the triggerzone, it pop out
So closing the ui allows you to interact properly with it
Time scale 0 should disable physics messages iirc
- hit the collision
- nothing pops out, timescale is 0, i cant move
- press escape which close the "imaginary UI"
- timescale is 1, i can move,
- reenter the vault collision
- vault UI pops out
a video demonstration
If you comment out the setting of the time scale to zero, does it work?
Cross eliminating potential issues
What gameobject is the VaultTip component on?
If not, it's to do with your active state of the object
sorry if I look stupid but wdym 😓
Does the log pop-up on first encounter?
You have this in Start()
vaultUI.SetActive(false);
If the GameObject with this component is disabled until you hit the collider.. it will auto close the vault UI
you meaan connected script?
yes
I put that line there cuz I want it to be false as default, it that wrong?
Update wouldn't occur though if it's inactive?
all good then
i have no clue
He's right it wouldn't. But if the gameobject this script is on is disabled at first, it could come on, stay on, turn off the UI and Update would run.
It's clear from the video vault is always on.
Just for good measures in debugging, try a different key for pausing and unpausing - tab or something. The escape key unfocus' the Editor which can result in strange behaviors.
youre suggesting to change the closeUI key to tab or other key?
Yeah, just for debugging in the Editor
same issue
chanaged it to tab
public void Update() {
if (Input.GetKeyDown(KeyCode.Tab)) {
closeUI();
}
}
Do you've got another script setting the object inactive?
uh yea? but it shouldnt be connect in any way
using System.Threading;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class vaultMech : vaultTip
{
[SerializeField]
private string password;
public TMP_Text vaultUIText;
public void grabPW(string input) {
password = input;
}
public void closeUIafter() {
Thread.Sleep(3000);
vaultUI.SetActive(false);
Time.timeScale = 1;
}
public void CheckPassword() {
switch (password)
{
case "COMBINE":
vaultUIText.text = "Door Unlocked";
closeUIafter();
break;
case "combine":
vaultUIText.text = "Door Unlocked";
closeUIafter();
break;
default:
vaultUIText.text = "Wrong Password";
closeUIafter();
break;
}
}
}
this is for checking the password
I mean, it could be connected in that this object needs to be active but isn't
if wrong or right it would closethe ui after 3 sec
this script is put under the tmp_inputfield
If you press pause first then touch the collider without touching first, does it work as expected?
nope also, changed it to k, spammed k 3 times, walk over, still no ui
should i try the most old-school way, which is just restart my unity?
So the initial touch is required. This implies something is done in the initial touch that allows it to work thereafter.
https://stackoverflow.com/questions/76130128/unity-setactivetrue-not-working-for-panel
there is this from stackoverflow a year before, tho im not sure what solution i should be looking for
It's either likely to do with the time scale or initial active state of the object - apparently it needs to be active in order to close and activate.
but if its a timescale issue shouldnt it literally never pop up? or is Start() causing the issue
It's not good to guess without educated deductions (we've deduced that it's necessary to be active initially to operate). Consider placing a log after the thread is slept to see if it's occurring immediately the first time.
Not sure why you're freezing the thread btw - hopefully not the main thread(!)
We're not supposed to assist with ai managed code tbh
It's a wild goose chase, rabbit's hole, etc
Best ask it to delay using a Unity Coroutine instead, good luck.
Consider starting a coroutine instead in closing the ui:cs IEnumerator CloseUIAfter(float duration) { yield return new WaitForSeconds(delay); vaultUI.Set... Time.timeScale... }Starting the coroutinecs StartCoroutine(CloseUIAfter(3f));You can
for more info.
Vault UI (Unsolved)
IEnumerator wait()
{
yield return new WaitForSeconds(3);
}
public void closeUIafter() {
StartCoroutine(wait());
vaultUI.SetActive(false);
Time.timeScale = 1;
}
something like this?
Or using mono behavior invoke if absolutely necessary (does not scale with game time and continues to operate even if object is disabled)cs Invoke("CloseUIAfter", 3);
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
alright i'll look into that :>
https://www.codinblack.com/how-to-run-a-method-at-certain-time-intervals-in-unity/ discussion on common ways to delay (not including async, multithreading, etc)
The last difference between Invoke and Coroutine which we will cover is the execution condition after the deactivation of the object. Invoke and InvokeRepeating do not stop after the game object is deactivated. On the other hand, this 'is' not true for coroutines. They stop after the game object is deactivated or destroyed. Therefore, you should use Invoke or InvokeRepeating, if you would like your method to continue running, even though the object is deactivated after the method is triggered.
using nameof() is better, with invoke.. so if the method name ever changes, it either auto updates (if you use the VS rename feature) or gives you an error so you know to update it
Invoke(nameof(CloseUIAfter), 3f);
Main issue
Consider placing a log after the thread is slept to see if it's occurring immediately the first time.
#1230148774198775818 message
oh I almost forgot this exist, but its fixed now. I basically changed nothing but heres the script now:
public class vaultTip : MonoBehaviour
{
public GameObject vaultUI;
public BoxCollider2D vault;
public void Start() {
vault = this.GetComponent<BoxCollider2D>();
vaultUI.SetActive(false);
}
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Player") {
vaultUI.SetActive(true);
}
}
private void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.tag == "Player") {
vaultUI.SetActive(false);
}
}
}
i just remove timescale and let the user move away to remove the vaultui, i think there is some buildtime(or something like that) issue where two lines of code happened at the same time, causing it not to appear or something. anyay thanks for your debugging skills :D