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

Automatic API Updater Ruining My Script

Discussion in 'Scripting' started by DerekBurias, Mar 21, 2016.

  1. DerekBurias

    DerekBurias

    Joined:
    Jan 27, 2016
    Posts:
    9
    I have a problem with my grapplinghook script. I used the camera component from my main camera in my grapplinghook script, the problem is unity says "Assets/LeftHook.js(11,15): BCE0144: 'UnityEngine.Component.camera' is obsolete. Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)", when I don't let it run Automatic API Updater, and if I do, the whole script goes wrong. How do I update my script without using the Automatic API Updater, and keep my script functional?
    Here is my script:
    Code (JavaScript):
    1. #pragma strict
    2. var speed : float;
    3. var isGrapple: boolean;
    4. var body : Transform;
    5.  
    6. function Start () {
    7.     var camera = GameObject.FindGameObjectWithTag("MainCamera");
    8. }
    9.  
    10. function Update () {
    11.     var ray = camera.GetComponent.<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    12.     var RH : RaycastHit;
    13.     if (Physics.Raycast(ray, RH, 200)){
    14.         var hit = RH.transform;
    15.     }
    16.     if (Input.GetButton("Fire2")){
    17.         isGrapple = true;
    18.     }
    19.     if (isGrapple == true){
    20.         transform.position = Vector3.MoveTowards(transform.position, hit.position, speed);
    21.         if (Input.GetButton("Jump")){
    22.             isGrapple = false;
    23.         }
    24.     }
    25. }
    I would appreciate it if you showed me the correct script, or if not possible, at least a solution.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    unity removed the quick accessors to a lot of components a while back, the reasoning for this can be found in the unity dev blogs around the release of 4.6

    The static reference to the main camera in the scene hasn't been removed, and given that that is what you appear to be trying to access I'd suggest removing the start function contents and replacing the reference in line 11 with

    Code (csharp):
    1.  
    2. ... = Camera.main.ViewportPointToRay( ....
    3.  
     
  3. DerekBurias

    DerekBurias

    Joined:
    Jan 27, 2016
    Posts:
    9
    Thanks, LeftyRighty.
     
  4. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    By the way, if you're curious about the issue you had, you declared your camera variable with the "var" keyword in the Start function, so the variable was scoped to the Start function.

    In your Update function, you didn't use the reference you set in Start since it no longer existed, instead you were using the old deprecated camera property.

    If you had declared a variabled outside the Start function, like you did for your other variables, you would have had a living reference to your camera which worked fine and as a bonus, no need to fight the API updater ^^.

     
    Kiwasi likes this.