This light's intensity won't change?

I have a script that functions as the player’s stamina bar. Here’s that…

#pragma strict
var staminaBar : GameObject;
var fadeTime : float = 0.20763;
var timerSpeed : float = 0;
var startPosition : float = 0;
var endPosition : float = 0;
var jumpStamina : float = 0;
var sprintTrigger : GameObject;
var staminaTimer : Light;
var player : CharacterController;

function Start ()
{
    staminaBar.GetComponent.<RectTransform>().anchoredPosition.x = startPosition;
}
function Update ()
{
    if (Input.GetButton("Sprint") && Input.GetAxis("Horizontal") || Input.GetButton("Sprint") && Input.GetAxis("Vertical"))
        if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x < endPosition)
            if (player.isGrounded)
        {
            staminaBar.GetComponent.<RectTransform>().anchoredPosition.x += 0.1 * Time.deltaTime / fadeTime;
        }
  
  

    if (sprintTrigger.activeSelf)
        if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x > startPosition)
            if(staminaTimer.GetComponent.<Light>().intensity == 0)
        {
            staminaBar.GetComponent.<RectTransform>().anchoredPosition.x -= 0.1 * Time.deltaTime / fadeTime;
        }

  
  
 
    if (Input.GetButton("Sprint") && Input.GetAxis("Horizontal") || Input.GetButton("Sprint") && Input.GetAxis("Vertical"))
        {
        sprintTrigger.SetActive (false);
        }
        else
        {
        sprintTrigger.SetActive (true);
        }

 
  
    if (Input.GetButtonDown("Jump"))
        if (player.isGrounded)
            if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x < endPosition)
        {
            staminaBar.GetComponent.<RectTransform>().anchoredPosition.x += jumpStamina;
            }

  
  
    if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x == endPosition)
    {
        staminaTimer.GetComponent.<Light>().intensity = 8;
    }

    staminaTimer.GetComponent.<Light>().intensity -= 0.1 * Time.deltaTime / timerSpeed;
}

I am having one issue though. The last statement isn’t working. The one where I want the intensity of the light to change to 8. I’m not sure why that is…

if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x == endPosition)
    {
        staminaTimer.GetComponent.<Light>().intensity = 8;
    }

Why is this not working?

probably due to floats imprecision (there’s a name for that i cant remember)

maybe Mathf.Approximately does what you need

How exactly would I do that?

since timerSpeed looks like never changed (0) it can be because of the divide by zero error

try setting timerSpeed to 1

if (Mathf.Approximately(staminaBar.GetComponent.<RectTransform>().anchoredPosition.x ,endPosition))
    {
        staminaTimer.GetComponent.<Light>().intensity = 8;
    }

your problem could be somewhere else, but comparing floats is never a good idea, they have an ‘‘imprecision’’ to them that can make two floats slightly different from one another, even though youd think they should be the same, for example, 0.1 + 0.1 can result in something like 0.2000001, instead of just 0.2

1 Like