Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by LBardmask · Nov 27, 2021 at 10:29 PM · uiraycasttriggers

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();
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

305 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I add UI pop up with this script? 0 Answers

Unity 5 UI not blocking Raycast Collider ( Physics Raycast ) 1 Answer

Raycast UI Elements 0 Answers

Unable to click UI button, tried everything I could find 1 Answer

Raycast Problem With enemy and objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges