Using a Slider component as a gauge in Unity.

Sometimes you are going to run into things that are fairly easy to do, but for some reason your code just won’t work. This was the case for me here. It really wasn’t that hard to implement this but for some reason I was getting error after error after error.

I’m going to be honest here — I really don’t know how I fixed it.

Another lesson for you — don’t ask questions. Just be happy with it :). I did a lot of changing from if statements to new methods, etc. Then, it just clicked.

Now you know I usually provide a lot of gif’s so you can see how things are done. I can’t do that here. There was just too many restarts and changes to record what actually worked. So I’m going to try to explain as best I can!

So first off you are going to create an image (call it border) inside of your canvas and make it the size of the slider you want. Then create another image and make it the same size, call this fill. Change the color of this to red — and make sure you have it set in the hierarchy so it is above the border. The border should be white, that way when the red slides it will show the background.

Now with that done, add an empty object and name it ThrusterBar. Put both images inside of this container. Add a Slider Componenet to the ThursterBar object and make sure the interactable check box is unchecked, transition to none and navigation to none. By doing those things you make sure it’s just a static object and you can’t interact with it.

Once we have that down we can create the code. First off, create a new C# script and attach it to the ThrusterBar object. Then open it and…

Now first, make sure you add the UI library at the top as we will be using the UI during this.

Next we just want to get access to the Slider component and giving it the name slider.

--

--

--

With Unity all can be accomplished!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Test Post from Add Crazy

What is MVC?

MVC components interacting with each other

Building a futuristic Record Player with Glitch and Raspberry Pi

Some Clean Code Examples in TypeScript

An Incredibly Gentle Introduction to D3.js

Logger decorator

Is TypeScript the future of JavaScript?

Hot to Use Break & Continue Statements in Javascript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Laine Dorchester

Laine Dorchester

With Unity all can be accomplished!

More from Medium

How To Use Post Processing In Unity

Object Pool Pattern — Design Patterns for Unity #3

Create a Control Room Game Scene: Part 2- Game Dev Series 148

Day 105: Working with Animation Events in Unity