Search Unity

Help with my lerping script.

Discussion in 'Scripting' started by Treasureman, Feb 14, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, so to start, I'm not super experienced with coding. I'm trying to make a script that makes a GameObject move from position 78.9 to position 0 if the light attached to the 'flashlight' variable is enabled and and it's intensity is greater than 0. Here's the script...
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var UI : GameObject;
    4. var flashLight : Light;
    5. var fadeTime : float = 0.20763;
    6.  
    7. function Start ()
    8. {
    9.     flashLight.GetComponent.<Light>().enabled = true;
    10.     UI.transform.position.y = 78.9;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     if(flashLight.GetComponent.<Light>().enabled == true){
    16.     }     if(flashLight.GetComponent.<Light>().intensity = > 0)
    17.  
    18.     {
    19.         UI.transform.position.y -= 0.1 * Time.deltaTime / fadeTime;
    20.         Debug.Log(flashLight.GetComponent.<Light>().intensity);
    21.     }
    22. }
    I'm not sure what's wrong. It's probably something to do with the brackets, but I'm not sure.
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Line 16. You are closing the contents if the conditional before the next part. So :
    Code (CSharp):
    1.     if(flashLight.GetComponent.<Light>().enabled == true){
    2.     }     if(flashLight.GetComponent.<Light>().intensity = > 0)
    3.     {
    4.         UI.transform.position.y -= 0.1 * Time.deltaTime / fadeTime;
    5.         Debug.Log(flashLight.GetComponent.<Light>().intensity);
    6.     }
    7.  
    should probably be:
    Code (CSharp):
    1.     if(flashLight.GetComponent.<Light>().enabled == true)
    2.     {
    3.         if(flashLight.GetComponent.<Light>().intensity = > 0)
    4.         {
    5.             UI.transform.position.y -= 0.1 * Time.deltaTime / fadeTime;
    6.             Debug.Log(flashLight.GetComponent.<Light>().intensity);
    7.         }
    8.     }    
    9.  
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I still get the same errors.
    Code (JavaScript):
    1. #pragma strict
    2. var UI : GameObject;
    3. var flashLight : Light;
    4. var fadeTime : float = 0.20763;
    5. function Start ()
    6. {
    7.     flashLight.GetComponent.<Light>().enabled = true;
    8.     UI.transform.position.y = 78.9;
    9. }
    10. function Update ()
    11. {
    12.   if(flashLight.GetComponent.<Light>().enabled == true)
    13.     {
    14.         if(flashLight.GetComponent.<Light>().intensity = > 0)
    15.         {
    16.             UI.transform.position.y -= 0.1 * Time.deltaTime / fadeTime;
    17.             Debug.Log(flashLight.GetComponent.<Light>().intensity);
    18.         }
    19.     }  
    20. }
    Here's what I get...

    Assets/Lighter UI.js(17,56): BCE0044: expecting ), found '='.
    Assets/Lighter UI.js(17,58): BCE0043: Unexpected token: >.
    Assets/Lighter UI.js(17,60): BCE0043: Unexpected token: 0.
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    The error indicates where the error is (a couple of lines off, presumably because you had some blank lines).
    on 14 you have :
    Code (CSharp):
    1. if(flashLight.GetComponent.<Light>().intensity = > 0)
    A conditional should be a comparison operation (see here).
    You have " = > " which pretty much means nothing to the interpretor, which is why it is throwing an error. I assume you want "greater than or equal to" Which would be ">=" (note, there are no spaces). Give that a try.
     
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    The script works, but for some reason, the position doesn't start at 78.9, but at 149.2419. Why? I changed >=0 to just >0 also.
    Code (JavaScript):
    1. #pragma strict
    2. var UI : GameObject;
    3. var flashLight : Light;
    4. var fadeTime : float = 0.20763;
    5. function Start ()
    6. {
    7.     flashLight.GetComponent.<Light>().enabled = true;
    8.     UI.transform.position.y = 78.9;
    9. }
    10. function Update ()
    11. {
    12.   if(flashLight.GetComponent.<Light>().enabled == true)
    13.     {
    14.         if(flashLight.GetComponent.<Light>().intensity >0)
    15.         {
    16.             UI.transform.position.y -= 0.1 * Time.deltaTime / fadeTime;
    17.             Debug.Log(flashLight.GetComponent.<Light>().intensity);
    18.         }
    19.     }  
    20. }
     
    Last edited: Feb 14, 2016