Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

This light's intensity won't change?

Discussion in 'Scripting' started by Treasureman, May 17, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that functions as the player's stamina bar. Here's that...
    Code (JavaScript):
    1. #pragma strict
    2. var staminaBar : GameObject;
    3. var fadeTime : float = 0.20763;
    4. var timerSpeed : float = 0;
    5. var startPosition : float = 0;
    6. var endPosition : float = 0;
    7. var jumpStamina : float = 0;
    8. var sprintTrigger : GameObject;
    9. var staminaTimer : Light;
    10. var player : CharacterController;
    11.  
    12. function Start ()
    13. {
    14.     staminaBar.GetComponent.<RectTransform>().anchoredPosition.x = startPosition;
    15. }
    16. function Update ()
    17. {
    18.     if (Input.GetButton("Sprint") && Input.GetAxis("Horizontal") || Input.GetButton("Sprint") && Input.GetAxis("Vertical"))
    19.         if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x < endPosition)
    20.             if (player.isGrounded)
    21.         {
    22.             staminaBar.GetComponent.<RectTransform>().anchoredPosition.x += 0.1 * Time.deltaTime / fadeTime;
    23.         }
    24.  
    25.  
    26.  
    27.     if (sprintTrigger.activeSelf)
    28.         if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x > startPosition)
    29.             if(staminaTimer.GetComponent.<Light>().intensity == 0)
    30.         {
    31.             staminaBar.GetComponent.<RectTransform>().anchoredPosition.x -= 0.1 * Time.deltaTime / fadeTime;
    32.         }
    33.  
    34.  
    35.  
    36.  
    37.     if (Input.GetButton("Sprint") && Input.GetAxis("Horizontal") || Input.GetButton("Sprint") && Input.GetAxis("Vertical"))
    38.         {
    39.         sprintTrigger.SetActive (false);
    40.         }
    41.         else
    42.         {
    43.         sprintTrigger.SetActive (true);
    44.         }
    45.  
    46.  
    47.  
    48.     if (Input.GetButtonDown("Jump"))
    49.         if (player.isGrounded)
    50.             if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x < endPosition)
    51.         {
    52.             staminaBar.GetComponent.<RectTransform>().anchoredPosition.x += jumpStamina;
    53.             }
    54.  
    55.  
    56.  
    57.     if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x == endPosition)
    58.     {
    59.         staminaTimer.GetComponent.<Light>().intensity = 8;
    60.     }
    61.  
    62.     staminaTimer.GetComponent.<Light>().intensity -= 0.1 * Time.deltaTime / timerSpeed;
    63. }
    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...
    Code (JavaScript):
    1. if (staminaBar.GetComponent.<RectTransform>().anchoredPosition.x == endPosition)
    2.     {
    3.         staminaTimer.GetComponent.<Light>().intensity = 8;
    4.     }
    Why is this not working?
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    probably due to floats imprecision (there's a name for that i cant remember)

    maybe Mathf.Approximately does what you need
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    How exactly would I do that?
     
  4. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    485
    since timerSpeed looks like never changed (0) it can be because of the divide by zero error
    try setting timerSpeed to 1
     
  5. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    Code (CSharp):
    1. if (Mathf.Approximately(staminaBar.GetComponent.<RectTransform>().anchoredPosition.x ,endPosition))
    2.     {
    3.         staminaTimer.GetComponent.<Light>().intensity = 8;
    4.     }
    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
     
    Last edited: May 18, 2016
    LeftyRighty likes this.