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

Accessing other scripts, JS. Is this a bug or I dont understand something?

Discussion in 'Editor & General Support' started by Ness, May 17, 2014.

  1. Ness

    Ness

    Joined:
    Oct 1, 2012
    Posts:
    182
    I have two scripts each on a different objects; one is on enemy character other is on a player that i control. General structure of scripts on both characters is almost the same.

    What I want to do is turn off a particular script with other script, both of which are on a same character so I do this:

    Code (csharp):
    1. var navMshPlayer = gameObject.GetComponent("NavMshPlayer");
    2. navMshPlayer.enabled = false;
    and it works

    Now I go to enemy character and i try to do the same thing

    Code (csharp):
    1. var navMeshEn = gameObject.GetComponent("NavMshEnemy");
    2. navMeshEn.enabled = false;
    and it does not work! It returns an error
    "BCE0019: 'enabled' is not a member of 'UnityEngine.Component'."

    After searching forums for a sollution I came with an idea to try this:

    Code (csharp):
    1. var navMeshEn = gameObject.GetComponent(NavMshEnemy);
    2. navMeshEn.enabled = false;
    And it worked!

    Can someone explain to me why sometimes I have to access a script like this:
    Code (csharp):
    1.  
    2. var navMeshEn = gameObject.GetComponent("NavMshEnemy");
    and sometimes like this:

    Code (csharp):
    1. var navMeshEn = gameObject.GetComponent(NavMshEnemy);
    I went throught all my scripts and all unity documentation for an answer to this question.
    What is the logic behind it? Is it a bug?
     
    Last edited: May 17, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    There's no bug; as described in the docs, using GetComponent with strings returns Component rather than the actual type. You should almost never use GetComponent with strings. The docs tell you the one case where you would ("for example when trying to access a C# script from Javascript"), but mixing languages in game scripts is also something you should avoid. Mixing languages is fine if you have utility scripts which are one-way, but when you start trying to interconnect game logic with GetComponent, that should all be one language.

    --Eric
     
  3. Ness

    Ness

    Joined:
    Oct 1, 2012
    Posts:
    182
    Yeah I`ve read the same thing, the problem is that when I try, instead of doing this:
    Code (csharp):
    1. var navMshPlayer = gameObject.GetComponent("NavMshPlayer");
    2. navMshPlayer.enabled = false;
    Do this:
    Code (csharp):
    1. var navMshPlayer = gameObject.GetComponent(NavMshPlayer);
    2. navMshPlayer.enabled = false;
    Compiler returns an error.
    So I was "forced" by a compiler to use string.
    No C# scripts in this project.
     
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Always specify the type for your variables. Leaving the compiler, or runtime, to work out the type is going to bite you. So, do:

    Code (csharp):
    1.  
    2. var navMshPlayer : NavMshPlayer = gameObject.GetComponent(NavMshPlayer) as NavMshPlayer;
    3. navMshPlayer.enabled = false;
    4.  
    should always work.
     
  5. Ness

    Ness

    Joined:
    Oct 1, 2012
    Posts:
    182
    Code (csharp):
    1. var navMshPlayer : NavMshPlayer = gameObject.GetComponent(NavMshPlayer) as NavMshPlayer;
    Does not work. Errors:

    Assets/Standard Assets/1MyFolder/Istats.js(36,36): BCE0018: The name 'NavMshPlayer' does not denote a valid type ('not found'). Did you mean 'UnityEditor.NavMeshBuilder'?
    Assets/Standard Assets/1MyFolder/Istats.js(36,92): BCE0018: The name 'NavMshPlayer' does not denote a valid type ('not found'). Did you mean 'UnityEditor.NavMeshBuilder'?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Well...in JS, GetComponent(Type) always returns Type; there's no need to specify Type repeatedly like that and it won't solve any problems. If you ever do GetComponent(Type) and get an error, it's because Type doesn't exist in that context. This is caused by mixing languages, or by mixing compiler stages caused by Magic Folders. i.e., a script in Standard Assets can't see scripts outside because it's compiled first. (Or it's caused by Type not actually existing anywhere....)

    --Eric
     
  7. Ness

    Ness

    Joined:
    Oct 1, 2012
    Posts:
    182
    Update: I`ve just noticed that this script is in Standart Assets folder which compiles differently AFAIK. I`ve moved it where other scripts are and this works now:
    Code (csharp):
    1.     var navMshPlayer : NavMshPlayer = gameObject.GetComponent(NavMshPlayer) as NavMshPlayer;
    :cool::cool::cool:

    Eric5h5, thx. In the past i`ve mixed C# with JS thats why I`ve used Standard Assets folder.