Retrieving the amount of scaling from the Canvas Scaler

Dear Unity users,

I am currently working on a healthbar which slowly drains the less health you have. I followed this tutorial to get the healthbars I wanted:

In short, it uses a mask layer and a seperate green bar to indicate the amount of health. By moving the green bar to the left, it disappears behind the mask layer and thus shows less and less health.
The method to calculate its position based on the amount of health is here:

float maxXValue = healthTransform.position.x;
float minXValue = healthTransform.position.x - healthTransform.rect.width;

private void HandleHealth()
    {
        Stats attachedStats = attachedObject.GetComponent<Stats>();

        float currentXValuePlayer = MapValues(attachedStats.currentHealth, 0, attachedStats.maxHealth, minXValue, maxXValue);
        healthTransform.position = new Vector3(currentXValuePlayer, cachedY);
    }

    private float MapValues(float x, float inMin, float inMax, float outMin, float outMax)
    {
        return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
    }
/*
EXPLANATION:
attachedStats.currentHealth is the current health the player has
attachedStats.maxHealth is the maximum amount of health the player can have
minXValue is the furthest left point the bar is at when health is 0
maxXValue is the furthest right point the bar is at when health is full
*/

This is the setting I use for the canvas the healthbar is drawn on.

The trouble is(probably) that because of the scaling the canvas does, healthTransform.rect.width still returns the original size of the healthbar and not the new, scaled size. So is there a way to find out how much the Canvas Scaler has scaled things?

A brilliant fellow found the answer for me on stackoverflow, should you ever run into the same problem and end up here just look at this thread: c# - Retrieving the amount of scaling from the Canvas Scaler (Unity) - Stack Overflow

Answer from stackoverflow should that link ever die:

I noticed this same problem, had scaleFactor 1.0 for a long time and was wondering what the problem was.

I’m running Unity 5.1.2 and I do get the number now.

canvasScaleFactor = tempLevelTitle.GetComponentInParent().scaleFactor;

(The topmost canvas.) I tried to get the scaleFactor while initialising the game, in the awake loops. I guess it’s 1.0 as long as the game isn’t still running - it doesn’t yet know what the scale factor will be before all the components are running. I now poll the factor just before the movement and I do get a sensible value…