raycast hit enables ui but it flickers rapidly
ok so i have a raycast that goes from the camera with a range of 2f with a layer mask for items and harvest items, when the raycast hits an item it enables the ui that tells the player they can pick up that item and when the raycast doesnt hit an item then the display is hidden, but instead of just enabling the ui it rapidly disables and enables it making a flickering effect. ive tried using trigger colliders but the effect is the same. also it behaves the same with both editor and build play. heres the complete code for the raycast script. i know theres a lot of iffy stuff regarding input, i just did what i needed to make it work for the time being.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemHover : MonoBehaviour
{
public float range = 5f;
public GameObject itemUI;
public Text ItemInfo;
public LayerMask mask;
public bool pickUp;
private DefaultInput defInput;
public InventoryObject inventory;
public string itemUIText;
public GroundItem gI;
public HarvestManager hM;
public Camera cam;
void Awake()
{
defInput = new DefaultInput();
defInput.Player.Interact.started += e => Clicked();
defInput.Player.Interact.canceled += e => UnClick();
defInput.Enable();
gI = null;
hM = null;
}
public void Update()
{
if (gI == null && hM == null)
{
CloseItemMessage();
}
else
{
if(gI != null)
{
OpenItemMessage(itemUIText);
PickUpItem(gI.item, gI.amount, gI);
}
else if(hM != null)
{
OpenItemMessage(itemUIText);
HarvestItem(hM.item, hM.amount, hM);
}
}
GetItemFromRay();
}
public void GetItemFromRay()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range, mask))
{
var gItem = hit.transform.GetComponent<GroundItem>();
var hItem = hit.transform.GetComponent<HarvestManager>();
if(gItem == null)
{
gI = null;
}
if(hItem == null)
{
hM = null;
}
if(gItem != null && gItem != gI)
{
gI = gItem;
if (gI.amount > 1)
itemUIText = gI.itemName + " (" + gI.amount + ")";
else
itemUIText = gI.itemName;
}
else if (hItem != null && hItem != hM)
{
hM = hItem;
if (hM.amount > 1)
itemUIText = hM.itemName + " (" + hM.amount + ")";
else
itemUIText = hM.itemName;
}
}
else
{
gI = null;
hM = null;
}
}
public void OpenItemMessage(string text)
{
itemUI.SetActive(true);
ItemInfo.text = text;
}
public void CloseItemMessage()
{
itemUI.SetActive(false);
}
public void Clicked()
{
if (pickUp)
return;
pickUp = true;
}
public void UnClick()
{
if (!pickUp)
return;
pickUp = false;
}
public void PickUpItem(ItemObject _item, int _amount, GroundItem gI)
{
if(pickUp)
{
inventory.AddItem(_item, _amount);
gI.PickedUp();
itemUI.SetActive(false);
UnClick();
}
}
public void HarvestItem(ItemObject _item, int _amount, HarvestManager hM)
{
if (pickUp)
{
inventory.AddItem(_item, _amount);
StartCoroutine(hM.Harvest());
itemUI.SetActive(false);
UnClick();
}
}
}
Answer by LBardmask · Nov 28, 2021 at 12:23 AM
ok so ive solved it. it was the update function. i switched to fixed update but that was worse, half the time it didnt work at all. when i switched to the lateupdate it worked perfectly. unfortunately now i have a new problem. while trying to fix this ui problem i added a few new things and now my builds dont load scenes correctly half the time. i think it has something to do with the load order of the hierarchy. sometimes i cant use the mouse buttons or sometimes tab and esc dont work. sometimes the player cant move at all. sometimes a standard cutout material gets switched to standard opaque, prefabs with rigidbodies dont have colliders so on and so forth, is there a way to manually set an ordered list to load a scenes contents from? also is there a way to have the timescale set to 0 while it loads then switch back to 1? (for rigidbodies and colliders to load so they dont fall through the ground)
Your answer
![](https://koobas.hobune.stream/wayback/20220613045920im_/https://answers.unity.com/themes/thub/images/avi.jpg)