Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Turning off Interactable UI's After Use

Discussion in 'Scripting' started by m_daniel26, May 26, 2023.

  1. m_daniel26

    m_daniel26

    Joined:
    Feb 7, 2023
    Posts:
    66
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class UsableInteractable : Interactable
    6. {
    7.     [Header("Animation Options")]
    8.     public Animator ANI;
    9.     public GameObject useText;
    10.  
    11.     public bool oneUse = false;
    12.  
    13.     [Header("Highlight Options")]
    14.     public GameObject normalObject;
    15.     public GameObject highlightedObject;
    16.  
    17.     private bool used;
    18.  
    19.     private bool inReach;
    20.  
    21.     void Start()
    22.     {
    23.         useText.SetActive(false);
    24.  
    25.         ANI.SetBool("inUse", false);
    26.  
    27.         normalObject.SetActive(true);
    28.         highlightedObject.SetActive(false);
    29.  
    30.         used = false;
    31.         inReach = false;
    32.     }
    33.  
    34.     public override void OnFocus()
    35.     {
    36.         if (!used)
    37.         {
    38.             inReach = true;
    39.             useText.SetActive(true);
    40.  
    41.             normalObject.SetActive(false);
    42.             highlightedObject.SetActive(true);
    43.         }
    44.  
    45.         else if (oneUse && used)
    46.         {
    47.             inReach = true;
    48.             useText.SetActive(false);
    49.  
    50.             normalObject.SetActive(true);
    51.             highlightedObject.SetActive(false);
    52.         }
    53.  
    54.         else if (!oneUse && used)
    55.         {
    56.             inReach = true;
    57.             useText.SetActive(true);
    58.  
    59.             normalObject.SetActive(false);
    60.             highlightedObject.SetActive(true);
    61.         }
    62.     }
    63.  
    64.     public override void OnInteract()
    65.     {
    66.         if (inReach)
    67.         {
    68.             UseFunction();
    69.         }
    70.     }
    71.  
    72.     public override void OnLoseFocus()
    73.     {
    74.         inReach = false;
    75.         useText.SetActive(false);
    76.  
    77.         normalObject.SetActive(true);
    78.         highlightedObject.SetActive(false);
    79.     }
    80.  
    81.     void UseFunction()
    82.     {
    83.         if (!used && inReach && Input.GetButtonDown("Interact"))
    84.         {
    85.             ANI.SetBool("inUse", true);
    86.          
    87.             useText.SetActive(false);
    88.  
    89.             normalObject.SetActive(true);
    90.             highlightedObject.SetActive(false);
    91.  
    92.             if (gameObject.tag == "Usable/Toilet")
    93.             {
    94.                 FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/ToiletHandle");
    95.             }
    96.  
    97.             else if (gameObject.tag == "Usable/Sink")
    98.             {
    99.                 FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/FaucetSqueak");
    100.             }
    101.  
    102.             else if (gameObject.tag == "Usable/Elevator1")
    103.             {
    104.                 FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/Elevator1");
    105.             }
    106.  
    107.             used = true;
    108.             inReach = false;
    109.  
    110.         }
    111.  
    112.         else if (!oneUse && used && inReach && Input.GetButtonDown("Interact"))
    113.         {
    114.             ANI.SetBool("inUse", true);
    115.            
    116.             useText.SetActive(false);
    117.  
    118.             normalObject.SetActive(true);
    119.             highlightedObject.SetActive(false);
    120.  
    121.             if (gameObject.tag == "Usable/Toilet")
    122.             {
    123.                 FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/ToiletHandle");
    124.             }
    125.  
    126.             else if (gameObject.tag == "Usable/Sink")
    127.             {
    128.                 FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/FaucetSqueak");
    129.             }
    130.  
    131.             else if (gameObject.tag == "Usable/Elevator1")
    132.             {
    133.                 FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Usables/Elevator1");
    134.             }
    135.  
    136.             used = true;
    137.             inReach = false;
    138.  
    139.         }
    140.  
    141.         else if (oneUse && used && inReach && Input.GetButtonDown("Interact"))
    142.         {
    143.             ANI.SetBool("inUse", false);
    144.            
    145.             useText.SetActive(false);
    146.  
    147.             normalObject.SetActive(true);
    148.             highlightedObject.SetActive(false);
    149.  
    150.             used = true;
    151.             inReach = false;
    152.         }
    153.     }
    154. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

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

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    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
    Code (csharp):
    1. if (!v[1] && v[2] && v[3]) // !used && inReach && Input.GetButton...
    2. {
    3.   return 0;
    4. }
    5.  
    6. else if (!v[0] && v[1] && v[2] && v[3]) // !oneUse && used && inReach && Input.GetButton...
    7. {
    8.   return 1;
    9. }
    10.  
    11. else if (v[0] && v[1] && v[2] && v[3]) // oneUse && used && inReach && Input.GetButton...
    12. {
    13.   return 2;
    14. }
    15.  
    16. return -1; // prints as "-"
    and got this
    Code (csharp):
    1. A  B  C  D  >
    2. .  .  .  .  -
    3. .  .  .  T  -
    4. .  .  T  .  -
    5. .  .  T  T  0
    6. .  T  .  .  -
    7. .  T  .  T  -
    8. .  T  T  .  -
    9. .  T  T  T  1
    10. T  .  .  .  -
    11. T  .  .  T  -
    12. T  .  T  .  -
    13. T  .  T  T  0
    14. T  T  .  .  -
    15. T  T  .  T  -
    16. T  T  T  .  -
    17. T  T  T  T  2
    where ABCD equals to
    Code (csharp):
    1. A = oneUse
    2. B = used
    3. C = inReach
    4. D = Input.GetButtonDown("Interact")
    Now does it work the way it's supposed to, I don't know! Only you can tell the specs.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    And here's OnFocus
    Code (csharp):
    1. if (!v[0]) // !used
    2. {
    3.   return 0;
    4. }
    5. else if (v[1] && v[0]) // oneUse && used
    6. {
    7.   return 1;
    8. }
    9. else if (!v[1] && v[0]) // !oneUse && used
    10. {
    11.   return 2;
    12. }
    13.  
    14. return -1;
    Code (csharp):
    1. A  B  >
    2. .  .  0
    3. .  T  0
    4. T  .  2
    5. T  T  1
    Code (csharp):
    1. A = used
    2. B = oneUse