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

[SOLVED] Cache GetComponent() into a Variable in JavaScript

Discussion in 'Scripting' started by difficultnerd, Jul 14, 2014.

  1. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    Hey Everyone,
    I'm trying to 'cache' my game components so that scripts run smoothly.

    According to the Performance Optimisations documentation (at http://docs.unity3d.com/412/Documentation/ScriptReference/index.Performance_Optimization.html) one of the things I should try and do is 'cache' references to my other scripts, to cut down on lookups when they're called.

    I have two scripts attached to the same game object, called Player. According to the documentation I can cache the other script's "Get Component" into a variable to make later use easier (and cut down on overhead).

    Following that pattern, I've tried this:

    Code (JavaScript):
    1.  
    2. #pragmastrict
    3. var Player : GameObject;
    4.  
    5. function Start () {
    6.  
    7. var PlayerMovement : AirplaneMovement = Player.GetComponent(AirplaneMovement) as AirplaneMovement;
    8.  
    9. }
    10.  
    11. function Update () {
    12.  
    13. PlayerMovement.RepositionAirplane(1,1);
    14.  
    15. }
    16.  
    But, for reasons I do not understand, I receive the following error:
    Assets/scripts/GetComponent.js(12,9): BCE0005: Unknown identifier: 'PlayerMovement'.



    So, assuming MyScript was a variable type I then tried:


    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4.  
    5. function Start () {
    6.  
    7.     var PlayerMovement : AirplaneMovement = GetComponent(AirplaneMovement) as AirplaneMovement;
    8.  
    9. }
    10.  
    11. function Update () {
    12.  
    13.     AirplaneMovement.RepositionAirplane(1,1);
    14.  
    15. }

    Which, for reasons I do not understand, I receive the following error:

    Assets/scripts/GetComponent.js(12,26): BCE0020: An instance of type 'AirplaneMovement' is required to access non static member 'RepositionAirplane'.


    I don't understand.

    Any ideas?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're declaring and setting the variables in the Start function... so they only exist within the Start function. If you want to cache the component for use elsewhere in the object's code you need to declare the variable outside any of the functions (in the same way as if you were opening a variable up to be used in the inspector).

    so in pseudo
    Code (csharp):
    1.  
    2. class someclass()
    3. {
    4.     var myVar : VarType
    5.  
    6.     Start()
    7.     {
    8.         myVar = GetComponent(...);
    9.     }
    10.  
    11.     Update(
    12.     {
    13.         myVar.function(...);
    14.     }
    15. }
    16.  
    17.  

    basically you've stumbled at the concept of "scope" if you want to look more into it.
     
  3. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    I originally thought it might be scope, but when I did this:

    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4.     var PlayerMovement : AirplaneMovement = GetComponent(AirplaneMovement) as AirplaneMovement;
    5.  
    6.  
    7. function Start () {
    8.  
    9.  
    10. }
    11.  
    12. function Update () {
    13.  
    14.     PlayerMovement.RepositionAirplane(1,1);
    15.  
    16. }
    I got this error:

    UnityException: You are not allowed to call this function when declaring a variable.
    Move it to the line after without a variable declaration.
    If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
    GetComponent..ctor () (at Assets/scripts/GetComponent.js:3)

    At which point i tried:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.     var PlayerMovement : AirplaneMovement;
    4.     PlayerMovement = GetComponent(AirplaneMovement) as AirplaneMovement;
    5.  
    6.  
    7. function Start () {
    8.  
    9.  
    10. }
    11.  
    12. function Update () {
    13.  
    14.     PlayerMovement.RepositionAirplane(1,1);
    15.  
    16. }
    And got the same error. At which point I went nuts and tried this:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.     var PlayerMovement : Component;
    4.  
    5.  
    6.  
    7. function Start () {
    8.         PlayerMovement = GetComponent(AirplaneMovement) as AirplaneMovement;
    9.    
    10. }
    11.  
    12. function Update () {
    13.  
    14.     PlayerMovement.RepositionAirplane(1,1);
    15.  
    16. }
    and got: Assets/scripts/GetComponent.js(14,24): BCE0019: 'RepositionAirplane' is not a member of 'UnityEngine.Component'.
     
  4. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    I have found an iteration that works!

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.     var PlayerMovement : AirplaneMovement;
    4.  
    5.  
    6.  
    7. function Start () {
    8.         PlayerMovement = GetComponent(AirplaneMovement) as AirplaneMovement;
    9.    
    10. }
    11.  
    12. function Update () {
    13.  
    14.     PlayerMovement.RepositionAirplane(1,1);
    15.  
    16. }
    Thanks for your help!
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    last one is right except PlayerMovement isn't of the type Component (well it is, but you want the most specific class not a base class further up the inheritance tree), it's a "AirplaneMovement" (or whatever your unityscript file is called)

    this is a declaration, "this variable exists"
    var myVar : varType

    this is an assignment "this variable is this thing"
    myVar = <something>

    you can say something exists outside of a function so the object knows that that thing exists and what type of thing it is, but you can only say "it's this thing" inside a function i.e. when the unity engine is running. (someone is probably going to prove me wrong now lol :) )
     
    difficultnerd likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    awesome, pipped me to the answer :)
     
  7. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    LeftyRighty - your explanation has made it make sense to me - thank you for taking the time! I feel like I learnt something that will be both awesome and terrifying :)