Refer UI Image's component?

I’m currently working on a script, to control a flashlight’s battery life, intensity, and the UI image representing it.

I’ve figured out pretty much everything. I’ve got almost everything working. But I need to refer to the UI Image’s “Image (Script)” component, to call upon its “Fill” value. Which is what I’m using to represent the battery life to the player.

I’ve looked around, and I do know how to refer to objects and their components, but not the UI elements.

Also, I do have the “using UnityEngine.UI;” extension, just fyi.

Should be just:

Image img = <gameObjectRef>.GetComponent<Image>();

Is “” literal, or should I replace it with something? Also, just in case, did you mess up the lower/upper case in it? I’ve never seen that being used, so I don’t know.

Also, when I put it in as is, it highlights the first “<>” part, and says "Parse Error: Unexpected symbol ‘<’ ".

P.S. Just for context purposes, the Image object I want to refer to is called “Life” (as in the battery life).

Okay, that’s not a real thing… let’s say you have this:

public Image ImageFill;  // in the inspector you drag the game object with the image(script) to this field

then later, you use:

ImageFill.fillAmount = // whatever you want here.

if you just have a gameObject reference or something not an image reference, then you could use the gameobject like in the script I wrote before, but obviously using that variable instead of

Hopefully that makes sense.

1 Like

Where should both of these be? Because currently I have the first one right inside the class (above both Update and Start, and the second one inside Update, along with other calculations, since I’m setting the fillAmount to lightLife (which is the battery life variable).

And the UI image isn’t changing when the battery is running out.

Would I need to still reference ImageFill’s component first? Or is the fillAmount automatically referring to the “Image (Script)” component?

The placement sounds right. Did you drag the item there from the hierarchy?
If you want you could paste a bit more code, in case something is off there…

Yes I did drag it from the hierarchy.

Here’s what I got, which is relevant:

    public Image ImageFill; //This is above both methods.

    void Update () {
        float flashPercentage = (5 / flashLight.intensity) * 100;
        float flashLife = flashPercentage / 100;
        ImageFill.fillAmount = flashLife;
    }

Just fyi, the flashLight.intensity is being decreased over time, higher up in the Update method, and that’s working fine.

This code is basically calculating the battery life percentage based on the light’s current intensity, and then converting it so that it’s the same amount as the possible fillAmount (0-1).

That way I can control the UI image at exactly the same speed as the actual light intensity.

Okay, well then just curious. if you Debug.Log the value of flashLife just before assigning it to the fillAmount, what value does it print out?

Aah that makes sense. The way I have it set, is it should start the flashLife at 1 since that’s 100% of the battery life (which it does). But then it should start decreasing it to 0 if the flashlight is turned on… Which it doesn’t.

It goes up, and then after a few seconds, it just says “Infinity”. :confused:

I have no idea what I’m doing wrong in the calculations.

And it doesn’t just go up. It goes up increasingly more quickly. Like when it gets to 9, it just starts going up by like 3 to 6 at a time, and once it gets to around 85, it says “infinity”.

I read your response, but it didn’t all sink in…
At least you know the issue now.
Try using some simple decreasing value for now, and then work out more precise math if you want after? :slight_smile: Just a suggestion… hopefully you’re on the right track now.

1 Like

I got it working.

The problem was I was dividing the maximum intensity (5), by the current intensity, to get the percentage. Where it should’ve been the other way around.

I never had a problem with understanding percentages, but I could never memorise the different conversion calculations either, so I was going off of this thread. And the person that first stated the calculation, got the two swapped the wrong way round.

It’s working now, and the battery image is changing based on the intensity.

Thank you again so much for your help. :slight_smile:

Sweet. Cool… :slight_smile: Enjoy

1 Like