I’m very sorry if this question was brought up like a thousand times but being quite new to Unity I didn’t manage to find the solution for my problem nor to comprehend what the problem is.
Basically, in a 2d environment I have a group of GameObjects ( cells ) that are sprites with a collider and a script attached to them. I want to detect when the mouse is pressed on each individual cell by using OnMouseDown() method. However, it seems that it works only for some of the cells. And if I move the camera even a bit, some of the cells stop working as well.
So, I have three questions:
- Am I going the right way? Are there simplier / safer ways?
- How can I fix it?
- What is causing the problem?
Code used for this:
public class BasicObject : MonoBehaviour {
void Start() {
createMenu = false;
}
void OnMouseDown() {
createMenu = true;
}
void OnMouseUp() {
createMenu = false;
}
void OnGUI() {
if ( createMenu ) {
GUI.Box( new Rect( 100, 100, 20, 20 ), "Hallelujah" );
}
}
[SerializeField] private string objectName;
private bool createMenu;
}
Picture of what cells are malfunctioning ( red don’t work at all, blue don’t work after I move camera )[87019-безымянныи.png|87019]