#Hiding UI Problem

1 messages · Page 1 of 1 (latest)

mossy fractal
#

I am making a inventory system with three inventory slots. My script does deactivate the UI from the items when i pick them up because I dont want it to show up when I hold them (ik the image of it says E but the keybind is F comfy ). (Please dont mind the way the script is written, I aint the best).
Everything works how it should, but when I change inventory slots and swap back to one I hold an item in, the UI of it shows again(evem tho it shouldnt show when its in my hand) and isnt able to deactivate again. I cant figure out how to solve it.

Under my player Gamobject is a empty Gameobject called Hand.
My cube looks like this:
Cube(InvetoryItemSystem, ItemUIUnHideScript)
Canvas(ItemUIShowScript)

The BoxCollider of the cube is still deactivated when I switch itemslots.

I think the problem is at the part of the Gameobject activating and deactivating again. Some one please help me blobOk . Thanks (I will send the scripts in the chat under this)

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

public class InventoryItemSystem : MonoBehaviour
{
    public GameObject ThisObject;
    public GameObject ItemUI;

    // Start is called before the first frame update
    void Start()
    {
        if (ThisObject.GetComponent<Collider>().enabled == false)
        {
            ItemUI.SetActive(false);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(ThisObject.GetComponent<Collider>().enabled == false)
        {
            ItemUI.SetActive(false);
        }
    }

    public void GetMeAsAnItem()
    {
        if (InventorySystem.activeSlot == 1)
        {
            if (InventorySystem.inventoryOne != null)
                InventorySystem.DropItem();
            InventorySystem.inventoryOne = ThisObject;
        }

        if (InventorySystem.activeSlot == 2)
        {
            if (InventorySystem.inventoryTwo != null)
                InventorySystem.DropItem();
            InventorySystem.inventoryTwo = ThisObject;
        }

        if (InventorySystem.activeSlot == 3)
        {
            if (InventorySystem.inventoryThree != null)
                InventorySystem.DropItem();
            InventorySystem.inventoryThree = ThisObject;
        }

        ThisObject.GetComponent<Collider>().enabled = false;

        ThisObject.GetComponent<Rigidbody>().freezeRotation = true;
        ThisObject.GetComponent<Rigidbody>().useGravity = false;

        ThisObject.transform.SetParent(InventorySystem.Hand.transform);
        ThisObject.gameObject.transform.rotation = InventorySystem.Hand.transform.rotation;
    }
}
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemUIShowScript : MonoBehaviour
{
    public GameObject player;
    public Vector3 distanceToPlayer;
    public int DistanceToHide = 6;
    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Main Camera");
    }

    // Update is called once per frame
    void Update()
    {
        //Decides if to show up or not
        CalculateDistance();

        if (distanceToPlayer.x > DistanceToHide || distanceToPlayer.z > DistanceToHide ||
            distanceToPlayer.y > 2)
        {
            gameObject.SetActive(false);
        }

        transform.LookAt(player.transform);
    }

    public void CalculateDistance()
    {
        distanceToPlayer = new Vector3(transform.position.x - player.transform.position.x,
            transform.position.y - player.transform.position.y
            , transform.position.z - player.transform.position.z);
        if (distanceToPlayer.x < 0)
            distanceToPlayer.x = distanceToPlayer.x * -1;
        if (distanceToPlayer.y < 0)
            distanceToPlayer.y = distanceToPlayer.y * -1;
        if (distanceToPlayer.z < 0)
            distanceToPlayer.z = distanceToPlayer.z * -1;

    }
}
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemUIUnHideScript : MonoBehaviour
{
    public GameObject ItemUI;
    public GameObject player;
    public int DistanceToShow = 6;
    public Vector3 distanceToPlayer;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        //Decides if to show up or not
        CalculateDistance();

        if (distanceToPlayer.x < DistanceToShow && distanceToPlayer.z < DistanceToShow && distanceToPlayer.y < 2)
        {
            ItemUI.gameObject.SetActive(true);
        }
    }

    public void CalculateDistance()
    {
        distanceToPlayer = new Vector3(transform.position.x - player.transform.position.x,
            transform.position.y - player.transform.position.y
            , transform.position.z - player.transform.position.z);
        if (distanceToPlayer.x < 0)
            distanceToPlayer.x = distanceToPlayer.x * -1;
        if (distanceToPlayer.y < 0)
            distanceToPlayer.y = distanceToPlayer.z * -1;
        if (distanceToPlayer.z < 0)
            distanceToPlayer.z = distanceToPlayer.z * -1;
    }
}
#

The InvetorySystemScipt is on my Player