Unity Help: Why Won’t My UI Elements Display Over Sprites?

Why Won’t My UI Elements Display Over Sprites?

This is the question I repeatedly asked myself as I tested my Pause functionality in Zombie Swipe. For some reason, the Pause UI elements were showing up beneath my sprites instead of on top of them. I had this nice dark overlay that covered the screen and “PAUSED” centered in big letters. But the Zombie sprites and Player sprite scoffed at these trivial objects and walked all over them.

What was really frustrating is that I knew this Pause UI worked when I was in the conceptualization stage and used regular Image components for my Player and (stationary) Zombies. But when I switched them out for actual sprites it stopped working. I knew that them being sprites was part of the problem, but not how to fix it. So here we go:

If you want UI elements to appear over sprites you have to put the UI elements in a separate canvas from the sprites. Previously, my sprites and Pause UI shared the same canvas.

Create a new canvas under your main canvas and name it appropriately. Place the UI elements you want to appear over sprites into it.

UI Canvas


Then, simply check the “Override Sorting” option on the canvas containing your UI elements.

Canvas sorting override


And now the Pause menu elements show up on top of the sprites.

Zombie Swipe Paused

If you have a better – or alternate – way of accomplishing the same thing let me know in the comments.

Zombie Swipe: Spawning Enemies in Unity – Iteration 4

Awhile back I shared some hideous code for spawning Zombies in Zombie Swipe. It did what I needed it to do, but I wasn’t thrilled with the structure of the code and knew it was a mess.

I discussed it with a programming friend and he gave me some pointers on how to clean it, including an outline of the code I would need to spawn enemies at certain intervals without using Coroutine. I had spent more time than I care to admit trying to get Zombies to spawn using a combination of While and Ifs and various timer variables.

First things first, I stopped using a list of Vector3 points to spawn Zombies. Instead, I switched to using a SpawnPoints game object and filled it with empty game objects all around the screen. In the code, spawnPosition is then determined by randomly picking one of the SpawnPoints’ children. Compared to my previous code, it is super clean and removes many unnecessary lines.Read More »

Zombie Swipe: Zombies Dropping Health Packs on Death

As I work more on Zombie Swipe I am trying to stay focused and implement the core elements of my initial vision first and foremost. But there are a couple things that have cropped up that interest me and I thought this one was pretty easy to implement the other night during a coding session. After squashing a few bugs I have it working pretty much how I want it to be, but will need to work on customizing it to be more useful.

What I wanted is for health packs to occasionally spawn every X number of Zombies slain by the Player. I knew I would need pieces of code that would:

  • Replace the Zombie sprite with a health pack sprite
  • Give the Player X amount of health when pack is clicked
  • Only spawn the health pack at a certain percentage rate

I used these two Unity Answers to help get an idea of what needed to be done:

How to spawn prefabs with percent random?

Instantiating droppable items on destroy game object

With those concepts combined I created a HealthPack class that would heal the player OnMouseDown() and stubbed in a red cross for the dropped health pack.

The results:


I am fairly pleased with how clean the code is, compared to some other parts of my code.

The SpawnHealthPack() method is called from the Update() method of the Zombie.cs class every time a Zombie dies.

public void SpawnHealthPack()
        while (zombieKillCount == zombieTarget)
            zombieTarget += targetIncrement; //Resets the zombieTarget amount so player
                                             //has to kill targetIncrement more  
                                             //zombies before next spawn chance

            Vector3 position = transform.position; //Get the Zombie object's current position

            if (Random.value <= 1) //Percent rate Health Pack spawns at. 
                GameObject healthpack = Instantiate(hpPack, position, Quaternion.identity);
                healthpack.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);


And the HealthPack class, thus far, is barebones and does its job.

public class HealthPack : MonoBehaviour

    Player player;

    public GameObject healthpack;

    public int hpUp = 5;

    void Start()
        player = GameObject.Find("Player").GetComponent<Player>();


    void OnMouseDown()

At some point I’d like to add animations to the health pack and destroyed Zombies. Until then, this will do.

Minecraft’s Happy Accidents of Programming

It fills me with hope whenever I think back on how Notch, even after Minecraft becoming a huge success, still had much to discover in the realm of programming and design.

Apparently, the foundation of Minecraft was developed in a week of frantic coding. From there it was years of growth and updates that made it into the widely popular game that would eventually be sold to Microsoft for astronomical sums of money.

Yet, well into the post-release development of Minecraft, Notch was still learning about how classes work. In a weekend of coding, he decided to add logic that would cause passive mobs to flee from players when attacked. This would make hunting for food all the more exciting. The result, however, was even more interesting than Notch had expected. Due to how the classes for passive mobs were set up, they not only ran from players when attacked but also from hostile mobs – such as wolves. This was a happy little accident that added an extra sense of depth and life to the world. It appeared that wolves would hunt sheep and the like. Nifty!

Minecraft Wolf
Wolf kills sheep and leaves behind wool.

But this is merely a side effect of how classes work. I can’t find a source to back this claim up, but I swear many years ago I read about Notch being excited to discover classes and implement them in Minecraft. To think that Notch had developed an extremely popular and profitable game without using classes (whether he was familiar with them or not) is incredible. Which is why it gives me hope that a good game can come from questionable code.

Back to doing some further testing. I was exploring a forest biome, hit upon a river cutting through it, and walked over a small hill. On the other side, a wolf was chasing a sheep, but the sheep was running away. I didn’t have this in mind when I wrote the code to make mobs flee, but it ended up giving me an unexpected experience of having some kind of echo system of competing behaviors in various animals, and it just felt.. nice. It made me realize we should explore more mob to mob interactions, and have more chaotic competing behavior. – You know what’s fun?




Zombie Swipe: Spawning Enemies in Unity – Iteration 3

I find that, as I learn my way around Unity and C#, I have a lot of ideas on how to problem solve and achieve my goals but not a lot of concrete knowledge on how to apply those ideas. I do not have a firm grasp on C# myriad of tools within its toolbox or even a strong sense of its syntax. But I am learning as I go and doing is something that makes learning fun for me.

For Zombie Swipe (working title) I wanted to create an enemy spawn system that would:

  • Randomly position zombie enemies outside of the camera’s view
  • Pull from a pool of zombie types
  • Increase in difficulty after so many waves of enemies

Read More »

Developing to Learn – For Fun

This may not be the right way – or even a good way – to write code, but I am learning as I go.

This blog is a place for me to share what I’m working on, things I’ve learned, and keep track of my development. I am learning to develop with C# scripting and Unity, primarily for hobbyist video game creation.

Or rather, I am developing to learn. With little formal training, I’m jumping right into it and learning as I do. Of course, I will supplement my doing with tutorials, reading, and online courses as time allows. But first and foremost I am interested in creating.

The real trick will be acknowledging my bad practices and not developing long term bad habits as I play with the code.
Read More »