Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Hide/Show my GUI (Inventory) with input

Discussion in 'Scripting' started by Slugg_dev, Apr 29, 2024.

  1. Slugg_dev

    Slugg_dev

    Joined:
    Apr 29, 2024
    Posts:
    15
    upload_2024-4-29_12-36-10.png
    well, it's a simple game, although I know in Game Maker, here in Unity things are completely different...
    I just wanted to know how I could hide this inventory screen when you press J/C, as it seems to be very different from hiding an element/object/sprite

    I have a script called ``inv_visible``, which was attached to the gameobject ``inventory``, in Hierarchy, that can controll the visibility of ``inv``
    upload_2024-4-29_12-42-29.png

    this is the code inside:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class inv_visible : MonoBehaviour
    7. {
    8.  
    9.     public GameObject pic;
    10.  
    11.     public void Trigger()
    12.     {
    13.  
    14.         if (pic.activeInHierarchy == false && Input.GetKeyDown(KeyCode.F))
    15.         {
    16.  
    17.             pic.SetActive(true);
    18.  
    19.         }
    20.         else
    21.         {
    22.  
    23.             pic.SetActive(false);
    24.  
    25.         }
    26.  
    27.     }
    28.  
    29. }
    30.  
    note: Inventory is a Canvas, and it has warnings like this because my laptop is weak.
    Sorry for the big text.
     

    Attached Files:

  2. SMahdiFaghih

    SMahdiFaghih

    Joined:
    Jul 13, 2019
    Posts:
    12
  3. Slugg_dev

    Slugg_dev

    Joined:
    Apr 29, 2024
    Posts:
    15
    Hnm, i see
    I will take a look
    thanks
    (I had forgotten that I had to call the methods, I almost tried to make a C# hook like Rain World)
     
    Last edited: Apr 29, 2024
  4. Slugg_dev

    Slugg_dev

    Joined:
    Apr 29, 2024
    Posts:
    15
    It worked,
    and thanks to one thing I forgot it MUST be called
    Thanks, and I adjusted the code too
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using player;
    5.  
    6. namespace inventorys
    7. {
    8.  
    9.     public class inv_visible : MonoBehaviour
    10.     {
    11.         [SerializeField]
    12.         public GameObject pic;
    13.         private bool is_enabled = false;
    14.  
    15.         public void Trigger()
    16.         {
    17.  
    18.             if (pic.activeInHierarchy == false && Input.GetKeyDown(KeyCode.F))                  //if press F (inventory)
    19.             {
    20.  
    21.                 is_enabled = true;                                                              //enabled
    22.  
    23.             }
    24.             else if (pic.activeInHierarchy == true && Input.GetKeyDown(KeyCode.F))             //if press F (inventory)
    25.             {
    26.  
    27.                 is_enabled = false;                                                             //disabled
    28.  
    29.             }
    30.  
    31.             if (is_enabled == false)                                                             //if disabled
    32.             {
    33.  
    34.                 pic.SetActive(false);                                                           //set to the active = true
    35.  
    36.             }
    37.             else if (is_enabled == true)                                                         //if enabled
    38.             {
    39.  
    40.                 pic.SetActive(true);                                                            //set to the active = true
    41.  
    42.             }
    43.  
    44.         void Update()
    45.         {
    46.  
    47.             Trigger();                                                                          //trigger the input
    48.  
    49.         }
    50.  
    51.     }
    52.  
    53. }
     
  5. SMahdiFaghih

    SMahdiFaghih

    Joined:
    Jul 13, 2019
    Posts:
    12
    I'm gald it worked. In order to make your code lceaner you can write your Trigger method like the code below and remove is_enabled.

    Code (CSharp):
    1. private void Trigger()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.F)
    4.     {
    5.         pic.SetActive(!pic.activeInHierarchy);
    6.     }
    7. }
    Also the comments don't really do anything or explain more, so you should avoid such comments in your code.
     
  6. Slugg_dev

    Slugg_dev

    Joined:
    Apr 29, 2024
    Posts:
    15
    Hmmmmmmm i see
    This could be very useful not only in this game, but also in my Rain World mod
    Except for the fact that I tend to explain everything with a comment, due to forgetfulness
    I can see
    Really thank you
     :) 
     
    Last edited: Apr 29, 2024
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,678
    The warnings have nothing to do with your laptop. The first message is just alerting you that more channels are enabled than may be required, and the second that you're in one color mode and the canvas is defaulting to the other color mode which may cause incorrect colors.
     
  8. Slugg_dev

    Slugg_dev

    Joined:
    Apr 29, 2024
    Posts:
    15
    I see
    OOF
    I never touched those parts, tbh
    like, it came from the factory
    Thanks for explaining that too
    >:)