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. Dismiss Notice

Java Script error help!

Discussion in 'Scripting' started by SD2020_, Mar 25, 2016.

  1. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    So i am making this game for College assingment and i am trying to make a Pick up flash light script. but i am having problems with this script that i have gotten help with.

    I have these 6 errors that i want to resolve but have a feeling that they have been updated since unity5.

    Can someone please help me fix these errors college tutor cant be contacted and need this resolved ASAP!!

    If i am been completely stupid please forgive, I am still learning.

    Thank you so much in advanced!


    Script:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var thisModel : GameObject;
    4. var myLight : Light;
    5.  
    6. private var getLight : boolean = false;
    7.  
    8. function Start ()
    9. {
    10.     myLight.light.enabled = false;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     var fwd = transform.TransformDirection(Vector3.forward);
    16.     var hit : RaycastHit;
    17.    
    18.     if(Physics.Raycast(transform.position, fwd, hit))
    19.     {
    20.         if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "PickUp")
    21.         {
    22.             getLight = true;
    23.    
    24.             if(Input.GetKeyDown("e"))
    25.             {
    26.                 Destroy(thisModel);
    27.                
    28.                 myLight.light.enabled = true;
    29.             }
    30.         }
    31.    
    32.         else
    33.         {
    34.             getLight = false;
    35.            
    36.             myLight.light.enabled = false;
    37.         }
    38.     }
    39. }
    Here are the errors:
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    You have two errors, and the error messages are self explanatory. Instead of using .light, use GetComponent<Light>. That's an easy fix. I don't think Components have ever had an "enabled" property unless you've added that to your actual component. You need to check the "active" property on the gameobject.
     
  3. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    How about the "Var thisModel : GameObject;"
    Should that have a GetComponent<GameObject>();?
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I don't see an error for that particular line... why would you think it is wrong? Sorry for the short answers, but as this is for a school assignment I think it helps you more for us to nudge you in the right direction and let you do some of the research and discover the complete answers. That's how you learn. Documentation is your friend.
     
  5. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Completely understand, been trying to get this working now for sometime and keep running into problems, like its asking me to put a semicolon where there is already one?
     
  6. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    I don't know much from Unity, but:
    • BCE0144 -> This error is telling you to replace something in your code with its suggestion because of an obsoleted component. In this case, replace .light by GetComponent<Light>().
    • BCE0019 -> .enabled doesn't exists in that instance. Maybe this wouldn't happen if you fix the other error.
     
    Dustin-Horne likes this.
  7. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Any changes i make solves the error put left me with two new errors that make no sense because one is an expected semicolon where there already is one and a missing bracket in the same location..
     
  8. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Because you're probably missing a bracket or semicolon up above somewhere. Your script isn't very long, just look at it line by line and see where it's missing. It should be easy to find.
     
  9. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    Yep, all sentences have to end with a semicolon (or bracket, depending what are you typing). And the errors should tell you the line and column where the problem is (numbers between parentheses, like Assets/Scripts/LightPickUp.js(28, 47) ).
     
  10. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Have a look here now:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var thisModel : GameObject;
    4. var myLight : getComponent.<Light>();
    5.  
    6. private var getLight : boolean = false;
    7.  
    8. function Start ()
    9. {
    10.     myLight.getComponent.<Light>().enabled = false;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     var fwd = transform.TransformDirection(Vector3.forward);
    16.     var hit : RaycastHit;
    17.    
    18.     if(Physics.Raycast(transform.position, fwd, hit))
    19.     {
    20.         if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "PickUp")
    21.         {
    22.             getLight = true;
    23.    
    24.             if(Input.GetKeyDown("e"))
    25.             {
    26.                 Destroy(thisModel);
    27.                
    28.                 myLight.getComponent.<Light>().enabled = true;
    29.             }
    30.         }
    31.    
    32.         else
    33.         {
    34.             getLight = false;
    35.            
    36.             myLight.getComponent.<Light>().enabled = false;
    37.         }
    38.     }
    39. }

    Please help man i honestly don't know what I am doing wrong..
     
  11. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Try clearing the error window and hitting Run to rebuild.
     
  12. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    You called wrong GetComponent<Light>() in line 4.
     
  13. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    I cant clear the error window, because the errors dont appear when i run the game, they are just static there..
     
  14. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    Click the Clear button in the Console log.
     
  15. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    I have and it doesnt clear it.
     
  16. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    Reopen Unity.
     
  17. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Made no difference.
     
  18. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    Show your code.
     
  19. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var thisModel : GameObject;
    4. var myLight : getComponent.<Light>();
    5.  
    6. private var getLight : boolean = false;
    7.  
    8. function Start ()
    9. {
    10.     myLight.light.enabled = false;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     var fwd = transform.TransformDirection(Vector3.forward);
    16.     var hit : RaycastHit;
    17.    
    18.     if(Physics.Raycast(transform.position, fwd, hit))
    19.     {
    20.         if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "PickUp")
    21.         {
    22.             getLight = true;
    23.    
    24.             if(Input.GetKeyDown("e"))
    25.             {
    26.                 Destroy(thisModel);
    27.                
    28.                 myLight.light.enabled = true;
    29.             }
    30.         }
    31.    
    32.         else
    33.         {
    34.             getLight = false;
    35.            
    36.             myLight.light.enabled = false;
    37.         }
    38.     }
    39. }
     
  20. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    You need the errors to?
     
  21. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    I said before this, and I don't see it in your code:
    And I also said this, but I dont see it (it would be in all places where you have it wrong spelled):
     
  22. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Actually, Shouldn't that just be:

    Code (csharp):
    1.  
    2. var myLight : Light;
    3.  
    That's a type declaration, not a setter. I'm not strong in UnityScript, but I think that just defines a type (like it does in TypeScript). You'll need to set the value elsewhere, and yes use the proper GetComponent not getComponent.
     
    Duri likes this.
  23. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    The only error line of code im getting now is
    PickUp.js(4,27): BCE0043: Unexpected token: <.

    That is after replaying all the .light's with getComponent<Light>()
     
  24. Duri

    Duri

    Joined:
    Dec 5, 2014
    Posts:
    29
    Yeah, I think the same: the variable would need to be setted. Maybe settings its value to a light associated with the GameObject (don't know how).