BCE0019: 'MouseLook' is not a member of 'function(System.Type): UnityEngine.Component'.

Everything worked fine but when I compiled the game I got this error :
BCE0019: ‘MouseLook’ is not a member of ‘function(System.Type): UnityEngine.Component’.
I have seen posts about this before but it just doesn’t help the answers just say enabled doesn’t work but they don’t offer a working solution.

The Code Is Below.

var cam1 : Camera;
var cam2 : Camera;
var cam3 : Camera;
var Player : GameObject;

function Update () {
     if(Input.GetKeyDown("1")){
    	  
          cam1.enabled = true;
          cam2.enabled = false;
          cam3.enabled = false;
          Player.GetComponent.MouseLook().enabled = true;  
          
     }
     if(Input.GetKeyDown("2")){
     	  
          cam1.enabled = false;
          cam2.enabled = true;
          cam3.enabled = false;
          Player.GetComponent.MouseLook().enabled = false;
          	
     }
     if(Input.GetKeyDown("3")){
    
     cam3.enabled = true;
     cam2.enabled = false;
     cam1.enabled = false;
     
    Player.GetComponent.MouseLook().enabled = false;
     
     }
}

1 Answer

1

The right way to use GetComponent in JS is as follows:

     Player.GetComponent(MouseLook).enabled = true;  

EDITED: You can avoid the classic “does not denote a valid type” error by getting MouseLook as a MonoBehaviour like this:

 var cam1 : Camera;
 var cam2 : Camera;
 var cam3 : Camera;
 var Player : GameObject;

 private var mLook: MonoBehaviour;
 
 function Start(){
     // get MouseLook as MonoBehaviour at Start:
     mLook = Player.GetComponent("MouseLook");
 }

 function Update () {
     if(Input.GetKeyDown("1")){
          cam1.enabled = true;
          cam2.enabled = false;
          cam3.enabled = false;
          mLook.enabled = true;  
     }
     if(Input.GetKeyDown("2")){
          cam1.enabled = false;
          cam2.enabled = true;
          cam3.enabled = false;
          mLook.enabled = false;
     }
     if(Input.GetKeyDown("3")){
          cam3.enabled = true;
          cam2.enabled = false;
          cam1.enabled = false;
          mLook.enabled = false;
     }
 }

Not quite...GetComponent(MouseLook), it's a type rather than a function.

Fixed it ;) btw it looks like he tried to use the generic version but the angle brackets are missing Player.GetComponent.<MouseLook>().enabled = false;

Thanks but i get a new error lol The name 'MouseLook' does not denote a valid type ('not found').

Shame on me! I copied the original line and forgot to remove the parenthesis... Thanks guys for the correction!

MouseLook is a C# script, and can't be seen by JS scripts during compilation (unless already compiled in a previous "wave"). You could place the JS script in some custom Assets subfolder like Assets/JsScripts, for instance. But there's a better way to solve the problem: you can get the script by its name and cast it to MonoBehaviour - take a look at my edited answer.