Resource management and avoiding "Update"

Hi guys. I want to avoid the “void update” since my script only needs to run when the user clicked something on the screen.

I would’ve used OnMouseDown but that would require me to do one huge collider. And besides I need “OnMouseDown” for other colliders in the scene so that’s not an option. I basically want my script to run as soon as it recognizes that something got clicked on while avoiding “void Update” since I don’t really need it to run every frame… is there such a thing?

yeah, OnMouseDown. I don’t see why that’s not an option just because it’s used elsewhere.

The new unity GUI system also supports events that are handled in an event driven manner as well.

Because imagine I have a bunch of objects in the scene. Each of those objects have a collider and a OnMouseDown script attached to them. Now I want a UI text script I have to only be activated once the player has clicked the mouse. If I use an OnMouseDown it wouldn’t work without a collider. If I put a collider it will block the other colliders.

Create a GameObject with a script that watches for the click - destroy the GameObject after the mouse is clicked.

One Update() running isn’t going to hurt your app. You’re optimizing prematurely.

Do you want the OnMouseDown for the other colliders to trigger even if the UI text script is in the way? As a user, I would find it confusing if I clicked on the UI text and it triggered an event on an object behind the text…

@ GroZZler - I accept the criticism that I might be optimizing prematurely. Still, I want to understand how to accomplish something like this. What do you mean by “watches for the click”? Can you be more specific?

@ Eisenpony - The UI text is not “in the way”, it’s always at the corner of the screen and it changes dynamically based on the clicked object.

Would Event Handler help in this situation?

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/events

1 Like

Yes, I think you should use events to hook up your UI text. When the selected object changes, trigger an event on a “selection manager” or something… Have your GUI text object register with the selection manager so it receives notification a new object was selected.

OK, I will keep tabs on it in the future. For now as you guys said I better stick with Update since it’s my only update in the scene anyway and it makes things easier for me :slight_smile: