I have a relatively simple scene with horizontal scrolling and grey box objects the player can click on that brings up a sprite based UI overlay. This should be easy setup, no problems, but then this is a Unity project where nothing easy ever really works like it’s supposed to…
My objects seem to randomly detect mouse collisions on startup. So I hacked a solution to disable and reenable the box collider 2d which seemed to fix the issue on the main screen. Then I added the UI with a full screen box collider 2d to mask mouse events for the main scene. Going back to the main scene, the mouse collision issues are back. More hack fixes involving off/on collider. Now I am adding buttons to the UI and there are again inconsistent issues and I am getting really really tired of hacking solutions.
Any ideas what is going on? When it works, everything is fine. When it doesn’t it seems disable and enable the collider fixes the problem. Is my project just haunted and I should start a new project? Should I burn OnMouseOver events in a fire and go with ray casting? Should I jump ship and head to Godot? Is there any way to actually test any of this in runtime, like viewing the collider info?
I am using an older version 2021.1.5f1 because it was the most stable version around when I was working on my last project and they haven’t added any features that make me think upgrading is really worth it (that and their version system is seriously confusing).
It is hard to tell since it is in the editor, but i have a few questions to hopefully help find the issue:
What exactly is the OnMouseOver function detecting? A 2d gameobject with a collider or a UI object?
When you say a full screen UI with a box collider 2d that mask events, did you put a collider 2d on the UI object (on the canvas) ?
Is your Canvas scaling with screen size?
Have you tried using OnPointerEnter/OnPointerExit instead of OnMouseOver? This has worked very well for me but I am only checking for UI objects, not objects NOT on the canvas
Hi Cornysam, thanks for the response. They moved this post from 2d to UGUI even though I am 100% not using UGUI…
A 2d object with a BoxCollider2D
It is a BoxCollider2D attached to a full screen rect that follows the camera with a Z-pos (and sorting layer) that places it in front of the other objects in the main screen.
There is no canvas involved. Unity, in their boundless wisdom, deemed this question that has nothing to do with UGUI to be in this forum instead of 2d
Nope. I would prefer not to work with UGUI at all outside of basic menus.
I was on vacation but im back. That full screen Rect that follows the camera feels really wonky. I would bet that the issue lies there somehow. I would debug and test the heck outta that. Does it rotate at all during play? Does it act weird when you click other colliders? Etc.
I think that Rect with the collider on the camera is blocking the other 2d colliders at odd times. Switching to Godot will not solve your issue lol. Unity isnt broken, you are just doing some wonky stuff
What would you recommend for the best way to handle overlapping sprites that need to handle mouse input with respect to the correct sorting order? The sorting order does not seem to do anything. It all seems to have to do with z-position - which is an insane and error-prone solution for a 2d project - but it is the solution Unity seems to provide. It seems that most people who want to do something so basic as having overlapping sprites is to use raycast and then use an event listener system to handle, which seems a massive overkill for the very basic project I am making. I would love to be the dummy here and there to be a very simple way to handle this. I mean Godot can manage to handle this without issue or complication
Then switch to Godot if it is so easy. Unity is just a tool like Godot or Unreal, each with their strengths and weaknesses. What is wrong with using Raycasts? If the project is small then performance and optimization shouldnt be a problem at all, so use Raycasts.
Godot does not have the multi-platform support I need for this project Mostly my problem with raycast is it is an ineloquent solution to a problem that I would really expect any basic 2d engine to support without this much hassle. I’ve worked with Unity a lot over the years and these little issues - and the ineloquent solutions they create - end up causing a lot of frustrations down the line. But I will just suck it up and do a workaround. Thanks for your help and patience
There is probably a better way, I just have never worked with overlapping sprites in that fashion. I wonder if some sort of deck/card game tutorial or post has handled issues like this since they have card sprites stacked on top of each other. I know they are just representations of data but there may be something to that. Good luck, sorry im kinda worthless
You helped out a lot. Just having someone to work out frustrations with is quite useful when tackling a seemingly random problem. I finally figured out the issue. To get a bounds for my scrolling I made a polygon collider which happened to be at the same z position as some of the stage objects, thus causing the seemingly random result. I feel dumb for missing this but also feel like this is a very easy problem for anyone to run into and that there should be a better solution provided by the engine (or at least an easy way to debug the issue).
All development, regardless of tool, usually has some dumb small thing we all miss. Whether a super complicated class and architecture or even something small. I still forget to add a Tag, or reset the child object’s position, or accidentally have a function calling just outside the proper curly brace. The more experienced we get, the faster we find these issues, and the less frequent they occur. But they definitely never go away.