Turning off Interactable UI's After Use

I have an interactable script that I have a couple variations of (doors/drawers, collectibles, etc.) To quickly break it down, when the player focuses on an object that they can interact with, a UI graphic prompt pops up and reminds them of the interact key, and the object highlights. Some objects can only be used/opened once, so I have a toggled bool to turn them off once they have been interacted with.

Standard stuff, and I’ve got it 99% working - however, the one thing I can’t quite get working is that if the object IS NOT a one-use object, I want the prompts and highlights to be deactivated after it’s been used, until the player looks away and looks at it again. If the object IS set to one-use, then it works perfectly.

In theory, I thought that toggling the object’s “inReach” bool to false and deactivating the prompts and highlights after interaction would achieve that - which it does…for one single frame, and then the prompts and highlights return to active. Is there something I’m missing?

Here’s the code for the “usable” iteration of the script - mainly used for sink faucets, buttons, etc - I chose this one as an example as it’s rather simpler than, for example, the doors and light switches iterations. Working in version 2021.3.20f1.

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

public class UsableInteractable : Interactable
{
    [Header("Animation Options")]
    public Animator ANI;
    public GameObject useText;

    public bool oneUse = false;

    [Header("Highlight Options")]
    public GameObject normalObject;
    public GameObject highlightedObject;

    private bool used;

    private bool inReach;

    void Start()
    {
        useText.SetActive(false);

        ANI.SetBool("inUse", false);

        normalObject.SetActive(true);
        highlightedObject.SetActive(false);

        used = false;
        inReach = false;
    }

    public override void OnFocus()
    {
        if (!used)
        {
            inReach = true;
            useText.SetActive(true);

            normalObject.SetActive(false);
            highlightedObject.SetActive(true);
        }

        else if (oneUse && used)
        {
            inReach = true;
            useText.SetActive(false);

            normalObject.SetActive(true);
            highlightedObject.SetActive(false);
        }

        else if (!oneUse && used)
        {
            inReach = true;
            useText.SetActive(true);

            normalObject.SetActive(false);
            highlightedObject.SetActive(true);
        }
    }

    public override void OnInteract()
    {
        if (inReach)
        {
            UseFunction();
        }
    }

    public override void OnLoseFocus()
    {
        inReach = false;
        useText.SetActive(false);

        normalObject.SetActive(true);
        highlightedObject.SetActive(false);
    }

    void UseFunction()
    {
        if (!used && inReach && Input.GetButtonDown("Interact"))
        {
            ANI.SetBool("inUse", true);
          
            useText.SetActive(false);

            normalObject.SetActive(true);
            highlightedObject.SetActive(false);

            if (gameObject.tag == "Usable/Toilet")
            {
                FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/ToiletHandle");
            }

            else if (gameObject.tag == "Usable/Sink")
            {
                FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/FaucetSqueak");
            }

            else if (gameObject.tag == "Usable/Elevator1")
            {
                FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/Elevator1");
            }

            used = true;
            inReach = false;

        }

        else if (!oneUse && used && inReach && Input.GetButtonDown("Interact"))
        {
            ANI.SetBool("inUse", true);
           
            useText.SetActive(false);

            normalObject.SetActive(true);
            highlightedObject.SetActive(false);

            if (gameObject.tag == "Usable/Toilet")
            {
                FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/ToiletHandle");
            }

            else if (gameObject.tag == "Usable/Sink")
            {
                FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/FaucetSqueak");
            }

            else if (gameObject.tag == "Usable/Elevator1")
            {
                FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/Elevator1");
            }

            used = true;
            inReach = false;

        }

        else if (oneUse && used && inReach && Input.GetButtonDown("Interact"))
        {
            ANI.SetBool("inUse", false);
           
            useText.SetActive(false);

            normalObject.SetActive(true);
            highlightedObject.SetActive(false);

            used = true;
            inReach = false;
        }
    }
}

Whoooeeey… that’s a lot of booleans to check!

I’ll just predict right now that one of them is incorrect, but I lack the brain cells to be able to parse what you want there.

Here’s how you can begin your investigation:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

I bet that your long if..else if chains in OnFocus and UseFunction don’t work the way you intended.

For example I analyzed your UseFunction method (by using this tool ) like this

if (!v[1] && v[2] && v[3]) // !used && inReach && Input.GetButton...
{
  return 0;
}

else if (!v[0] && v[1] && v[2] && v[3]) // !oneUse && used && inReach && Input.GetButton...
{
  return 1;
}

else if (v[0] && v[1] && v[2] && v[3]) // oneUse && used && inReach && Input.GetButton...
{
  return 2;
}
 
return -1; // prints as "-"

and got this

A  B  C  D  >
.  .  .  .  -
.  .  .  T  -
.  .  T  .  -
.  .  T  T  0
.  T  .  .  -
.  T  .  T  -
.  T  T  .  -
.  T  T  T  1
T  .  .  .  -
T  .  .  T  -
T  .  T  .  -
T  .  T  T  0
T  T  .  .  -
T  T  .  T  -
T  T  T  .  -
T  T  T  T  2

where ABCD equals to

A = oneUse
B = used
C = inReach
D = Input.GetButtonDown("Interact")

Now does it work the way it’s supposed to, I don’t know! Only you can tell the specs.

And here’s OnFocus

if (!v[0]) // !used
{
  return 0;
}
else if (v[1] && v[0]) // oneUse && used
{
  return 1;
}
else if (!v[1] && v[0]) // !oneUse && used
{
  return 2;
}

return -1;
A  B  >
.  .  0
.  T  0
T  .  2
T  T  1
A = used
B = oneUse