#Vault UI (Resolved)

1 messages · Page 1 of 1 (latest)

pallid thorn
#
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

patent fox
#

So closing the ui allows you to interact properly with it

#

Time scale 0 should disable physics messages iirc

pallid thorn
#
  1. hit the collision
  2. nothing pops out, timescale is 0, i cant move
  3. press escape which close the "imaginary UI"
  4. timescale is 1, i can move,
  5. reenter the vault collision
  6. vault UI pops out
#

a video demonstration

patent fox
#

If you comment out the setting of the time scale to zero, does it work?

#

Cross eliminating potential issues

pallid thorn
#

first collider still doesnt show

#

second collide shows

stable hemlock
#

What gameobject is the VaultTip component on?

patent fox
#

If not, it's to do with your active state of the object

pallid thorn
patent fox
stable hemlock
#

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

pallid thorn
#

you meaan connected script?

pallid thorn
pallid thorn
stable hemlock
#

it depends where the component is 😄

#

(this script)

patent fox
#

Update wouldn't occur though if it's inactive?

pallid thorn
#

its on vault

#

not on the panel at least

stable hemlock
#

all good then

pallid thorn
stable hemlock
#

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.

patent fox
#

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.

pallid thorn
#

youre suggesting to change the closeUI key to tab or other key?

patent fox
#

Yeah, just for debugging in the Editor

pallid thorn
#

same issue

#

chanaged it to tab

#
    public void Update() {
        if (Input.GetKeyDown(KeyCode.Tab)) {
            closeUI();
        }
    }
patent fox
#

Do you've got another script setting the object inactive?

pallid thorn
#

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

patent fox
#

I mean, it could be connected in that this object needs to be active but isn't

pallid thorn
#

if wrong or right it would closethe ui after 3 sec

#

this script is put under the tmp_inputfield

patent fox
pallid thorn
#

should i try the most old-school way, which is just restart my unity?

patent fox
#

So the initial touch is required. This implies something is done in the initial touch that allows it to work thereafter.

pallid thorn
patent fox
#

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.

pallid thorn
#

but if its a timescale issue shouldnt it literally never pop up? or is Start() causing the issue

patent fox
#

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(!)

pallid thorn
#

this thread?

#

wait i didnt know i could do that

patent fox
pallid thorn
#

oh

#

haha

patent fox
#

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.

pallid thorn
#

uh oh

#

thanks for reminder :/

#

hope this wouldnt go unfixed

patent fox
#

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 google for more info.

pallid thorn
#

Vault UI (Unsolved)

#
    IEnumerator wait()
    {
        yield return new WaitForSeconds(3);
    }

    public void closeUIafter() {
        StartCoroutine(wait());
        vaultUI.SetActive(false);
        Time.timeScale = 1;
    }

something like this?

patent fox
pallid thorn
patent fox
#

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.

stable hemlock
#

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);

patent fox
pallid thorn
#

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