How to fix this problem in my game?

I am currently working on a game in defold. Its one of those country games. But I have a problem/bug in it that makes no sense to me and have been unable to fix. In my game whenever you hover your mouse over a country it will show the name of the country and what continent it is in. Here is a picture:


In the picture it seems normal, but for some reason the hover sprite will sometimes go behind the country. Sometimes when I run the game, it will be on top of the country (Which is how it supposed to be) but sometimes if I close and run the game again the hover sprite will appear behind the country. (This happens randomly)
Here is an example:

I have set the z value of the ocean game object in the very back to 0, the countries 1, and the hover game object to 2. The game object should always be on top of the countries. Can someone explain why it sometimes go behind the country and sometimes it doesn’t??


The hover game object will always follow the mouse.
Whenever the mouse is not touching a country, the game object will be disabled and when you touch a country it will enable.

If I recall correctly by default z-values are spaced only from 0 to 1. You can change it in settings somewhere. I just use float values, like 0.5. You can try to set Ocean to 0, countries to 0.5 and hover sign to 0.8 or any other value more than country and less than 1

1 Like

The default render script will render from -1 to 1 in Z, so you need to stay between those values.

3 Likes

If that code snippit above is for the hover object, you’re setting its Z value to 0 every frame, which will override whatever you have set for the object in the editor.

Also remember that the GO has a Z value, and the sprite also has a Z value, which is additive.

2 Likes